添加具有不同大小输入和输出的 Keras Lambda 层时的广播问题
Broadcasting issue when adding Keras Lambda layer with different size input and output
我搜索并发现了类似的问题,但 none 这似乎与我面临的问题相同。我正在尝试使用 Theano 后端(都是最新的)通过 Keras 实现一个神经网络,它涉及一个 Lambda 层,该层获取一个层的一维输出,并将其转换为一个 n 维向量,其中包含 1- d输出重复n次。
我似乎 运行 遇到的问题是,在 Lambda 层,Keras 似乎期望输入具有与我指定的输出形状相同的维度:
x=Input(shape=(2,))
V1=Dense(1)(x)
V2=Lambda(lambda B : B[0,0]*K.ones((3,)),output_shape=(3,))(V1)
model=Model(inputs=x,outputs=V2)
rms = RMSprop()
model.compile(loss='mse', optimizer=rms)
model.predict(np.array([1,2]).reshape((1,2)))
给出了这个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-40a7e91d5963> in <module>()
----> 1 model.predict(np.array([1,2]).reshape((1,2)))
/Users/user/anaconda/envs/py35/lib/python3.5/site-packages/keras/engine /training.py in predict(self, x, batch_size, verbose)
1504 f = self.predict_function
1505 return self._predict_loop(f, ins,
-> 1506 batch_size=batch_size, verbose=verbose)
1507
1508 def train_on_batch(self, x, y,
/Users/user/anaconda/envs/py35/lib/python3.5/site-packages/keras/engine/training.py in _predict_loop(self, f, ins, batch_size, verbose)
1137
1138 for i, batch_out in enumerate(batch_outs):
-> 1139 outs[i][batch_start:batch_end] = batch_out
1140 if verbose == 1:
1141 progbar.update(batch_end)
ValueError: could not broadcast input array from shape (3) into shape (1)
我知道还有其他方法可以尝试这样做 (K.repeat_elements
) 但这也给了我有关广播的错误消息。请注意,即使我删除 B[0,0]*
问题仍然存在(这样 Lambda 层根本不依赖于 B
)。如果我将 K.ones
和 output_shape
中的 (3,)
更改为 (1,)
那么它似乎可以工作。
据我了解,Lambda 层应该能够处理 input/output 对不同维度,对吗?
在output_shape
中,你没有考虑batch size。所以这是正确的:(3,)
但在张量中,batch size 并没有被忽略。您的表达式结果至少需要两个维度:(Batch_size,3)。
另外,不要使用张量的元素,使用整个张量。我还没有发现使用单独的元素很重要或有用的情况(因为你应该对整个批次执行完全相同的操作)
我建议你使用K.repeat_elements(B, rep=3, axis=-1)
我搜索并发现了类似的问题,但 none 这似乎与我面临的问题相同。我正在尝试使用 Theano 后端(都是最新的)通过 Keras 实现一个神经网络,它涉及一个 Lambda 层,该层获取一个层的一维输出,并将其转换为一个 n 维向量,其中包含 1- d输出重复n次。
我似乎 运行 遇到的问题是,在 Lambda 层,Keras 似乎期望输入具有与我指定的输出形状相同的维度:
x=Input(shape=(2,))
V1=Dense(1)(x)
V2=Lambda(lambda B : B[0,0]*K.ones((3,)),output_shape=(3,))(V1)
model=Model(inputs=x,outputs=V2)
rms = RMSprop()
model.compile(loss='mse', optimizer=rms)
model.predict(np.array([1,2]).reshape((1,2)))
给出了这个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-40a7e91d5963> in <module>()
----> 1 model.predict(np.array([1,2]).reshape((1,2)))
/Users/user/anaconda/envs/py35/lib/python3.5/site-packages/keras/engine /training.py in predict(self, x, batch_size, verbose)
1504 f = self.predict_function
1505 return self._predict_loop(f, ins,
-> 1506 batch_size=batch_size, verbose=verbose)
1507
1508 def train_on_batch(self, x, y,
/Users/user/anaconda/envs/py35/lib/python3.5/site-packages/keras/engine/training.py in _predict_loop(self, f, ins, batch_size, verbose)
1137
1138 for i, batch_out in enumerate(batch_outs):
-> 1139 outs[i][batch_start:batch_end] = batch_out
1140 if verbose == 1:
1141 progbar.update(batch_end)
ValueError: could not broadcast input array from shape (3) into shape (1)
我知道还有其他方法可以尝试这样做 (K.repeat_elements
) 但这也给了我有关广播的错误消息。请注意,即使我删除 B[0,0]*
问题仍然存在(这样 Lambda 层根本不依赖于 B
)。如果我将 K.ones
和 output_shape
中的 (3,)
更改为 (1,)
那么它似乎可以工作。
据我了解,Lambda 层应该能够处理 input/output 对不同维度,对吗?
在output_shape
中,你没有考虑batch size。所以这是正确的:(3,)
但在张量中,batch size 并没有被忽略。您的表达式结果至少需要两个维度:(Batch_size,3)。
另外,不要使用张量的元素,使用整个张量。我还没有发现使用单独的元素很重要或有用的情况(因为你应该对整个批次执行完全相同的操作)
我建议你使用K.repeat_elements(B, rep=3, axis=-1)