Keras深度自编码器结构中的NoneTypeError / 形状错误

我遇到了让我的神经网络进行训练的问题。我定义的神经网络如下:

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)

其示意图如下所示 enter image description here

嵌入层定义如下:

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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注