简体   繁体   English

Keras中不同的输入/输出形状

[英]Different Input/Output shape in Keras

I'm new to all this Neural Networks thing and I'm actually trying some toy codes with different codig options (raw Python, TF...) 我是所有这些神经网络的新手,我实际上尝试了一些具有不同codig选项的玩具代码(原始Python,TF ......)

Currently, I've made a simple binary AND, OR and NOT operator solving network in TFLearn: 目前,我在TFLearn中创建了一个简单的二进制AND,OR和NOT运算符求解网络:

# 1. Import library of functions
import numpy as np
import tflearn
from keras.models import Sequential
from keras.layers import Dense, Activation

# 2. Logical data
input = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]]
YOR = [[0.], [1.], [1.], [1.]]
YAND=[[0.], [0.], [0.], [1.]]
YNOT=[[0.], [1.], [1.], [0.]]

######   VERSION TFLEARN     #####
# 3. Building our neural network/layers of functions 
neural_net = tflearn.input_data(shape=[None, 2])
neural_net = tflearn.fully_connected(neural_net, 1, activation='sigmoid')
neural_net = tflearn.regression(neural_net, optimizer='sgd', learning_rate=2, loss='mean_square')

# 4. Train the neural network / Epochs
model = tflearn.DNN(neural_net,tensorboard_verbose=0)
model.fit(input, YOR, n_epoch=1000, snapshot_epoch=False)

# 5. Testing final prediction
print("Testing OR operator")
print("0 or 0:", model.predict([[0., 0.]]))
print("0 or 1:", model.predict([[0., 1.]]))
print("1 or 0:", model.predict([[1., 0.]]))
print("1 or 1:", model.predict([[1., 1.]]))

Now I'm trying to replicate it in Keras (using CNTK backend) using this code: 现在我正在尝试使用此代码在Keras中复制它(使用CNTK后端):

# 2. Logical OR operator / the data
input = np.array([[0., 0.], [0., 1.], [1., 0.], [1., 1.]])
YOR = np.array([[0.], [1.], [1.], [1.]])
YAND=np.array([[0.], [0.], [0.], [1.]])
YNOT=np.array([[0.], [1.], [1.], [0.]])

######   VERSION KERAS     #####
# 3. Building our neural network/layers of functions 
model= Sequential()
model.add(Dense(4,input_shape=[2,]))
model.add(Activation('sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# 4. Train the neural network / Epochs
model.fit(input,YOR,epochs=1000,verbose=1)

# 5. Testing final prediction
print("Testing OR operator")
print("0 or 0:", model.predict([[0., 0.]]))
print("0 or 1:", model.predict([[0., 1.]]))
print("1 or 0:", model.predict([[1., 0.]]))
print("1 or 1:", model.predict([[1., 1.]]))

On execution, I would expect to obtain the result of the operator in each case, but instead I got the following error: 在执行时,我希望在每种情况下获得运算符的结果,但我得到以下错误:

ValueError: Error when checking input: expected dense_1_input to have shape (2,) but got array with shape (1,)

According to Keras Doc , seems to be that the output shape must be the same as the input shape, and though I can modify the input_shape, apparently doesn't recognize the output_shape arg. 根据Keras Doc ,似乎输出形状必须与输入形状相同,虽然我可以修改input_shape,但显然无法识别output_shape arg。

By the way if I try to change the value of the input_shape in order to fit it to the output (according to what i just mention) I get the same message but swapping those values. 顺便说一句,如果我尝试更改input_shape的值以使其适合输出(根据我刚才提到的)我得到相同的消息但交换这些值。

Does this mean that I can only obtain results of the same shape as the input? 这是否意味着我只能获得与输入相同形状的结果?

I tried running the program you have given. 我试过运行你给的程序。 But it produced a different type of error for me 但它给我带来了不同类型的错误

Error when checking target: expected activation_13 to have shape (4,) but got array with shape (1,) 检查目标时出错:预期activation_13具有形状(4,)但是得到了具有形状的数组(1,)

I changed value inside Dense to solve the above error. 我在Dense中更改了值以解决上述错误。 Why don't you try using this 你为什么不尝试使用它

model= Sequential()
model.add(Dense(1,input_shape=(2,)))
model.add(Activation('sigmoid'))

model.compile(optimizer='rmsprop',
          loss='binary_crossentropy',
          metrics=['accuracy'])

# 4. Train the neural network / Epochs
model.fit(input,YOR,epochs=1000,verbose=1)

# 5. Testing final prediction
print("Testing OR operator")
test = np.array([[0., 0.]])
print("0 or 0:", model.predict(test))
test = np.array([[0., 1.]])
print("0 or 1:", model. model.predict(test))
test = np.array([[1., 0.]])
print("1 or 0:",  model.predict(test))
test = np.array([[1., 1.]])
 print("1 or 1:",  model.predict(test))

Also we can train models in Keras even if the input and output shape are different 即使输入和输出形状不同,我们也可以在Keras中训练模型

I want to add something to the already given answer. 我想在已经给出的答案中添加一些内容。 Because you actually can keep the line as it was with 4 units resp. 因为你实际上可以保持线路,因为它是4单位resp。 hidden size: 隐藏大小:

model.add(Dense(4, input_shape=(2,))) 

So assuming you want to keep a hidden size of 4 , then you need to add just an proper output layer where the shape is matching the shape of your data. 因此,假设您希望保持隐藏的大小为4 ,那么您需要添加一个正确的输出图层,其中形状与数据的形状相匹配。

In you case: 在你的情况下:

model.add(Dense(1))

So if you want to keep the hidden size different than 1 this is probably what you want, here is the full working code: 因此,如果你想保持隐藏的大小不同于1这可能是你想要的,这是完整的工作代码:

Note: I also added another activation for the output layer. 注意:我还为输出层添加了另一个激活。

import numpy as np

from keras.models import Sequential
from keras.layers import Dense, Activation

# 2. Logical OR operator / the data
input = np.array([[0., 0.], [0., 1.], [1., 0.], [1., 1.]])
YOR = np.array([[0.], [1.], [1.], [1.]])
YAND=np.array([[0.], [0.], [0.], [1.]])
YNOT=np.array([[0.], [1.], [1.], [0.]])

######   VERSION KERAS     #####
# 3. Building our neural network/layers of functions 
model= Sequential()
model.add(Dense(4, input_shape=(2,)))
# you can place 
model.add(Activation('sigmoid'))
# layer to match output shape
model.add(Dense(1))
# of course you can add a sigmoid or other 
# activation here to match you target value range
model.add(Activation('sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# 4. Train the neural network / Epochs
print(input.shape)
model.fit(input,YOR,epochs=1000,verbose=1)

# 5. Testing final prediction
print("Testing OR operator")
print("0 or 0:", model.predict([[0., 0.]]))
print("0 or 1:", model.predict([[0., 1.]]))
print("1 or 0:", model.predict([[1., 0.]]))
print("1 or 1:", model.predict([[1., 1.]]))

I hope this makes things clearer and helps you to understand the error message better. 我希望这会使事情更清晰,并帮助您更好地理解错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM