关于keras.model的一些随记(一)
import numpy as np from keras.models import Sequential from keras.layers import Dense from keras.optimizers import Adam
model = Sequential() model.add(Dense(48, input_dim=11, activation=relu)) # 要求输入为1维,replay的输入就要注意一下。 model.add(Dense(48, activation=relu)) model.add(Dense(48, activation=relu)) model.add(Dense(2, activation=relu)) #后改的。 model.compile(loss=mse, optimizer=Adam(lr=0.01))
# state = np.array([[6,6,6,6,6,6,6,6,6,6,6]]) #(1,11)其中1代表样本数目,11对应model中的租后一层维度。 # target_f = np.array([[6,7]]) #(1,11)其中1代表样本数目,2对应model中的最后一层维度。
state = np.array([[6,6,6,6,6,6,6,6,6,6,6],[6,6,6,6,6,6,6,6,6,6,6],[6,6,6,6,6,6,6,6,6,6,6]]) #(1,11)其中1代表样本数目,11对应model中的租后一层维度。 target_f = np.array([[6,7],[6,6],[7,7]]) #(1,11)其中1代表样本数目,2对应model中的最后一层维度。 model.fit(state, target_f, epochs=10, verbose=0)
# 对data的shape的要求:只要第二维是11就好了。 # data = np.array([[7,7,7,7,7,7,7,7,7,7,7]]) # (1,11) """[[8.281055 0. ]] """ data = np.array([[7,7,7,7,7,7,7,7,7,7,7],[7,7,7,7,7,7,7,7,7,7,7]]) #(2,11) """ [[5.738095 8.590874] [5.738095 8.590874]] """ out = model.predict(data) print(out)
# 小结: # 1.输入和输出最好都是二维:shape[0]可以理解为样本数目,shape[1]可以理解为每一个样本的形状。 # 2.在fit训练的过程中,目标的个数和训练集的数目要一致。