我有二维形状的样品
(say [a, 10])
. 其中a从一个样本变为另一个样本。我正在和
batch size = 1
避免批量大小可变的问题。我创造了如下
LSTM
网络。现在问题是我的目标是形状的概率向量
[1,a,1]
. 每个样本的概率向量之和为1。
我想申请
softmax
激活最后一层,这样我就可以与目标进行比较。我该怎么办?
Layer (type) Output Shape Param #
==========================================================================================
lstm_21 (LSTM) (1, None, 32) 7808
__________________________________________________________________________________________
lstm_22 (LSTM) (1, None, 8) 1312
__________________________________________________________________________________________
time_distributed_6 (TimeDistributed) (1, None, 1) 9
==========================================================================================
这是我的密码
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.LSTM(32, return_sequences=True, batch_input_shape=(1, None, len(features))))
model.add(tf.keras.layers.LSTM(8, return_sequences=True))
model.add(tf.keras.layers.Dense(1, activation='softmax'))
# model.add(tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(1, activation='softmax')))
print(model.summary(90))
model.compile(loss = 'mean_squared_error',
optimizer = 'adam')
def generate_arrays_from_pd(df, arr_df):
while True:
for i in range(arr_df.shape[0]):
a1 = arr_df[i, 0]
a2 = arr_df[i, 1]
batch_x = df.loc[a1:a2, features].as_matrix().reshape((1, -1, len(features)))
batch_y = df.loc[a1:a2, "mkt_shr"].as_matrix().reshape((1, -1, 1))
yield(batch_x, batch_y)
model.fit_generator(generate_arrays_from_pd(dat_train, arr_train), steps_per_epoch=arr_train.shape[0], epochs = 10, verbose=1, shuffle=False)