无法使用多输入输入运行嵌入网络Keras

时间:2018-11-20 10:08:49

标签: tensorflow keras

我尝试运行一个带有9个输入的嵌入层的简单keras模型。但是我总是会遇到两个错误,具体取决于嵌入后的层。 我尝试使用2种不同的数据表示形式,但是得到了相同的结果。 现在,我所拥有的:

1。我正在使用自己的健身生成器,该生成器会生成数据:

(list of shapes of input data) -
[(25,), (25,), (25,), (25, 24), (25, 11), (25, 10), (25, 28), (25, 8), (25, 7)] 


features = [['id1',1], ['id2',1],
 ['id3',1], ['id4',24],
 ['id5',11], ['id6',10], ['id7',28], ['id8',8], ['id9',7]]

embeddings = []
inputs = []
for idx, feature in enumerate(features):
      meta_input = Input(shape=(feature[1],), name = feature[0] + '_input')
      sqrt = int(np.sqrt(feature[1]))

      embedding = Embedding(feature[1], 1, input_length=1,name = feature[0] + '_embed')(meta_input)
      fl = Flatten()(embedding)
      embeddings.append(fl)
      inputs.append(meta_input)

x = Concatenate()(embeddings)    
dense_meta_1 =  Dense(256, activation='relu')(x) #x
drop_meta = Dropout(0.2)(dense_meta_1)

dense_meta_2 = Dense(1)(drop_meta)


model = Model(inputs, dense_meta_2)

model.compile(optimizer='Adam', loss='mean_squared_error', metrics= 
['mae']) 
history = model.fit_generator(my_gen_v2(batch_size, batch_folder, steps), epochs=1, steps_per_epoch=steps,
                         max_queue_size=1)

所以当我使用扁平化图层时-我收到了此消息(部分内容):

  

InvalidArgumentError:矩阵大小不兼容:In [0]:[25,91],In [1]:[9,256]        [[node density_25 / MatMul(定义在/home/human/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:1076)= MatMul [T = DT_FLOAT,_class = [“ loc:@ training_7 / Adam / gradients / dense_25 / MatMul_grad / MatMul“],transpose_a = false,transpose_b = false,_device =” / job:localhost /副本:0 / task:0 / device:GPU:0“](concatenate_16 / concat, density_25 /内核/读取)]]        [[{{nodemetrics_11 / mean_absolute_error / Mean_1 / _1219}} = _Recvclient_terminated = false,recv_device =“ / job:localhost /副本:0 / task:0 / device:CPU:0”,send_device =“ / job:localhost /副本:0 /任务:0 /设备:GPU:0“,send_device_incarnation = 1,tensor_name =” edge_1116_metrics_11 / mean_absolute_error / Mean_1“,tensor_type = DT_FLOAT,_device =” / job:本地主机/副本:0 /任务:0 /设备:CPU:0“]]

但是当我使用重塑图层时:

embedding = Reshape(target_shape=(1,), name = feature[0] + '_reshape')(embedding)

我知道了:

  

InvalidArgumentError:要整形的输入是具有600个值的张量,但请求的形状有25个        [[node race_reshape / Reshape(定义在/home/human/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:1898)= Reshape [T = DT_FLOAT,Tshape = DT_INT32,_device =“ / job:本地主机/副本:0 /任务:0 /设备:GPU:0“](race_embed_16 / GatherV2,race_reshape / Reshape / shape)]]        [[{{nodemetrics_12 / mean_absolute_error / Mean_1 / _1417}} = __Recvclient_terminated = false,recv_device =“ / job:localhost /副本0 / task:0 / device:CPU:0”,send_device =“ / job:localhost /副本:0 /任务:0 /设备:GPU:0“,send_device_incarnation = 1,tensor_name =” edge_1098_metrics_12 / mean_absolute_error / Mean_1“,tensor_type = DT_FLOAT,_device =” /作业:本地主机/副本:0 /任务:0 /设备:CPU:0“]]

关于stackoverflow没有类似的问题,仅关于图像形状。请帮我解决这个问题,因为我为此花了很多时间(

1 个答案:

答案 0 :(得分:0)

通过更改嵌入层中的input_length以输入要素的形状(在我的示例中为feature [1])解决了该问题