我遇到了让我的神经网络进行训练的问题。我定义的神经网络如下:
shared = embedding_layerinputA = keras.Input(shape=(40, ), name="anchor") # 变长整数序列inputP = keras.Input(shape=(40, ), name="positive") # 变长整数序列inputN = keras.Input(shape=(40, ), name="negative") # 大小为num_tags的二进制向量concatenated = layers.concatenate([inputA, inputP, inputN])embedded_A = shared(concatenated)encoded = Dense(900, activation = "relu")(embedded_A)encoded = Dense(600, activation = "relu")(encoded)encoded = Dense(300, activation = "relu")(encoded)encoded = Dense(100, activation = "relu")(encoded)decoded = Dense(100, activation = "relu")(encoded)decoded = Dense(300, activation = "relu")(decoded)decoded = Dense(600, activation = "relu")(decoded)decoded = Dense(900, activation = "relu")(decoded)predictionsA = Dense(40, activation="sigmoid", name ='outA')(decoded)predictionsP = Dense(40, activation="sigmoid", name ='outB')(decoded)predictionsN = Dense(40, activation="sigmoid", name ='outC')(decoded)ml_model = keras.Model( inputs=[inputA, inputP, inputN], outputs=[predictionsA, predictionsP, predictionsN])ml_model.compile( optimizer='adam', loss='mse')ml_model.fit( {"anchor": anchor, "positive": positive, "negative": negative}, {"outA": anchor, "outB": positive, 'outC': negative}, epochs=2)
嵌入层定义如下:
embedding_m = model.syn0embedding_layer = Embedding(len(vocab), 300, weights=[embedding_m], input_length=40, trainable=True)
我输入网络的是三个形状为(120000, 40)的numpy数组,类似于这样:array([[ 2334, 23764, 7590, ..., 3000001, 3000001, 3000001], [3000000, 1245, 1124, ..., 3000001, 3000001, 3000001], [ 481, 491, 5202, ..., 3000001, 3000001, 3000001], ..., [3000000, 125, 20755, ..., 3000001, 3000001, 3000001], [1217971, 168575, 239, ..., 9383, 1039, 87315], [ 12990, 91, 258231, ..., 3000001, 3000001, 3000001]])
输入与输出是相同的,因为我正在构建一个自编码器解码器。
我得到的错误是:
维度必须相等,但对于'{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](model_3/outA/Sigmoid, mean_squared_error/Cast)’来说,维度分别为120和32,输入形状为:[32,120,40], [32,40]。
但我无法找出原因,也不知道如何修复…有什么想法吗?如果需要,我可以提供更多示例。我怀疑有些维度错误,因为我希望我的输出形状与输入完全相同,即(120000,40)。
回答:
修复后的编码器 – 解码器版本:
import tensorflow as tffrom tensorflow import kerasfrom tensorflow.keras import layersfrom keras.layers import Dense#shared = embedding_layer#模拟...shared=Dense(1,activation="relu")inputA = keras.Input(shape=(40, ), name="anchor") # 变长整数序列inputP = keras.Input(shape=(40, ), name="positive") # 变长整数序列inputN = keras.Input(shape=(40, ), name="negative") # 大小为num_tags的二进制向量concatenated = layers.concatenate([inputA, inputP, inputN])embedded_A = shared(concatenated)encoded = Dense(900, activation = "relu")(embedded_A)encoded = Dense(600, activation = "relu")(encoded)encoded = Dense(300, activation = "relu")(encoded)encoded = Dense(100, activation = "relu")(encoded)#decoded = Dense(100, activation = "relu")(encoded)decoded = Dense(300, activation = "relu")(encoded)decoded = Dense(600, activation = "relu")(decoded)decoded = Dense(900, activation = "relu")(decoded)predictionsA = Dense(40, activation="sigmoid", name ='outA')(decoded)predictionsP = Dense(40, activation="sigmoid", name ='outB')(decoded)predictionsN = Dense(40, activation="sigmoid", name ='outC')(decoded)ml_model = keras.Model( inputs=[inputA, inputP, inputN], outputs=[predictionsA, predictionsP, predictionsN])ml_model.compile( optimizer='adam', loss='mse')#模拟...anchor=tf.random.uniform((100,40))positive=tf.random.uniform((100,40))negative=tf.random.uniform((100,40))ml_model.fit( {"anchor": anchor, "positive": positive, "negative": negative}, {"outA": anchor, "outB": positive, 'outC': negative}, epochs=2)