keras理解(一)

inputs = Input(shape=(None, 4))#shape=(?, ?, 4),生成3维的矩阵,若后面的层是LSTM层,则必须是3维的矩阵
encoder_inputs = Input(shape=(4))#这种写法会报错,必须加逗号
encoder_inputs = Input(shape=(4,))#shape=(?, 4),生成2维的矩阵
致力于在仅仅知道输入和输出的情况下创建模型,例如,若a,b,c是张量tensor, 则可以

model = Model(input=[a, b], output=c)
源码:
def Input(shape=None, batch_shape=None,
          name=None, dtype=None, sparse=False,
          tensor=None):

参数:
shape,不包括batch size, 例如 shape=(32,), 意味着输入是1行32列的向量
batch_shape:指batch size, 例如batch_shape=(10,32),意味着 输入的batch=10,即实际输入为10行,32列的矩阵, batch_shape=(None,32)意味着任意batch的32列向量
sparse: 布尔值,指出被创建的占位符placeholder是否是稀疏的。

个人觉得这个input函数就是tensorflow得占位符作用,也不知道理解得对不对相当于
inputs=tf.placeholder(tf.int32,[batch_size, 10],name='input')
例如:
```python
        # this is a logistic regression in Keras
        x = Input(shape=(32,))
        y = Dense(16, activation='softmax')(x)
        model = Model(x, y)

LSTM,
encoder = LSTM( units=4, return_state=True)

参数:
units:正整数,输出的维数
activation:默认激活函数为tanh,若指定activation=None, 就没有应用激活函数,即采用的线性激活,a(x)=x
use_bias:布尔值,是否采用偏置
kernel_initializer:初始化核kernel的权重矩阵
recurrent_initializer:初始化recurrent_kernel的权重矩阵
dropout: 默认为0,用户可取0-1之间的float值,Fraction of the units to drop for  the linear transformation of the inputs.
return_state:布尔值,默认为False, 即只返回output,,若用户指定 return_state=True,则返回最后状态output+last state
return_sequences:布尔值, 默认为False,只输出last output,若用户指定 return_sequences=True, 则返回full sequence
if return_sequences:
            output_shape = (input_shape[0], input_shape[1], output_dim)
        else:
            output_shape = (input_shape[0], output_dim)

猜你喜欢

转载自blog.csdn.net/weixin_38145317/article/details/79549406
今日推荐