Keras reports TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
I'm a beginner in Keras and just write a toy example. It reports a TypeError
. The code and error are as follows:
Code:
inputs = keras.Input(shape=(3, ))
cell = keras.layers.SimpleRNNCell(units=5, activation='softmax')
label = keras.layers.RNN(cell)(inputs)
model = keras.models.Model(inputs=inputs, outputs=label)
model.compile(optimizer='rmsprop',
loss='mae',
metrics=['acc'])
data = np.array([[1, 2, 3], [3, 4, 5]])
labels = np.array([1, 2])
model.fit(x=data, y=labels)
Error:
Traceback (most recent call last):
File "/Users/david/Documents/code/python/Tensorflow/test.py", line 27, in <module>
run()
File "/Users/david/Documents/code/python/Tensorflow/test.py", line 21, in run
label = keras.layers.RNN(cell)(inputs)
File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/layers/recurrent.py", line 619, in __call__
...
File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py", line 473, in __call__
scale /= max(1., (fan_in + fan_out) / 2.)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
So how can I deal with it?
python tensorflow machine-learning keras rnn
add a comment |
I'm a beginner in Keras and just write a toy example. It reports a TypeError
. The code and error are as follows:
Code:
inputs = keras.Input(shape=(3, ))
cell = keras.layers.SimpleRNNCell(units=5, activation='softmax')
label = keras.layers.RNN(cell)(inputs)
model = keras.models.Model(inputs=inputs, outputs=label)
model.compile(optimizer='rmsprop',
loss='mae',
metrics=['acc'])
data = np.array([[1, 2, 3], [3, 4, 5]])
labels = np.array([1, 2])
model.fit(x=data, y=labels)
Error:
Traceback (most recent call last):
File "/Users/david/Documents/code/python/Tensorflow/test.py", line 27, in <module>
run()
File "/Users/david/Documents/code/python/Tensorflow/test.py", line 21, in run
label = keras.layers.RNN(cell)(inputs)
File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/layers/recurrent.py", line 619, in __call__
...
File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py", line 473, in __call__
scale /= max(1., (fan_in + fan_out) / 2.)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
So how can I deal with it?
python tensorflow machine-learning keras rnn
add a comment |
I'm a beginner in Keras and just write a toy example. It reports a TypeError
. The code and error are as follows:
Code:
inputs = keras.Input(shape=(3, ))
cell = keras.layers.SimpleRNNCell(units=5, activation='softmax')
label = keras.layers.RNN(cell)(inputs)
model = keras.models.Model(inputs=inputs, outputs=label)
model.compile(optimizer='rmsprop',
loss='mae',
metrics=['acc'])
data = np.array([[1, 2, 3], [3, 4, 5]])
labels = np.array([1, 2])
model.fit(x=data, y=labels)
Error:
Traceback (most recent call last):
File "/Users/david/Documents/code/python/Tensorflow/test.py", line 27, in <module>
run()
File "/Users/david/Documents/code/python/Tensorflow/test.py", line 21, in run
label = keras.layers.RNN(cell)(inputs)
File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/layers/recurrent.py", line 619, in __call__
...
File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py", line 473, in __call__
scale /= max(1., (fan_in + fan_out) / 2.)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
So how can I deal with it?
python tensorflow machine-learning keras rnn
I'm a beginner in Keras and just write a toy example. It reports a TypeError
. The code and error are as follows:
Code:
inputs = keras.Input(shape=(3, ))
cell = keras.layers.SimpleRNNCell(units=5, activation='softmax')
label = keras.layers.RNN(cell)(inputs)
model = keras.models.Model(inputs=inputs, outputs=label)
model.compile(optimizer='rmsprop',
loss='mae',
metrics=['acc'])
data = np.array([[1, 2, 3], [3, 4, 5]])
labels = np.array([1, 2])
model.fit(x=data, y=labels)
Error:
Traceback (most recent call last):
File "/Users/david/Documents/code/python/Tensorflow/test.py", line 27, in <module>
run()
File "/Users/david/Documents/code/python/Tensorflow/test.py", line 21, in run
label = keras.layers.RNN(cell)(inputs)
File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/layers/recurrent.py", line 619, in __call__
...
File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py", line 473, in __call__
scale /= max(1., (fan_in + fan_out) / 2.)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
So how can I deal with it?
python tensorflow machine-learning keras rnn
python tensorflow machine-learning keras rnn
edited Nov 27 '18 at 9:48
today
11k22037
11k22037
asked Nov 27 '18 at 9:08
daviddavid
487
487
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The input to a RNN layer would have a shape of (num_timesteps, num_features)
, i.e. each sample consists of num_timesteps
timesteps where each timestep is a vector of length num_features
. Further, the number of timesteps (i.e. num_timesteps
) could be variable or unknown (i.e. None
) but the number of features (i.e. num_features
) should be fixed and specified from the beginning. Therefore, you need to change the shape of Input layer to be consistent with the RNN layer. For example:
inputs = keras.Input(shape=(None, 3)) # variable number of timesteps each with length 3
inputs = keras.Input(shape=(4, 3)) # 4 timesteps each with length 3
inputs = keras.Input(shape=(4, None)) # this is WRONG! you can't do this. Number of features must be fixed
Then, you also need to change the shape of input data (i.e. data
) as well to be consistent with the input shape you have specified (i.e. it must have a shape of (num_samples, num_timesteps, num_features)
).
As a side note, you could define the RNN layer more simply by using the SimpleRNN
layer directly:
label = keras.layers.SimpleRNN(units=5, activation='softmax')(inputs)
Thanks for your answer!
– david
Nov 27 '18 at 9:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53496095%2fkeras-reports-typeerror-unsupported-operand-types-for-nonetype-and-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The input to a RNN layer would have a shape of (num_timesteps, num_features)
, i.e. each sample consists of num_timesteps
timesteps where each timestep is a vector of length num_features
. Further, the number of timesteps (i.e. num_timesteps
) could be variable or unknown (i.e. None
) but the number of features (i.e. num_features
) should be fixed and specified from the beginning. Therefore, you need to change the shape of Input layer to be consistent with the RNN layer. For example:
inputs = keras.Input(shape=(None, 3)) # variable number of timesteps each with length 3
inputs = keras.Input(shape=(4, 3)) # 4 timesteps each with length 3
inputs = keras.Input(shape=(4, None)) # this is WRONG! you can't do this. Number of features must be fixed
Then, you also need to change the shape of input data (i.e. data
) as well to be consistent with the input shape you have specified (i.e. it must have a shape of (num_samples, num_timesteps, num_features)
).
As a side note, you could define the RNN layer more simply by using the SimpleRNN
layer directly:
label = keras.layers.SimpleRNN(units=5, activation='softmax')(inputs)
Thanks for your answer!
– david
Nov 27 '18 at 9:55
add a comment |
The input to a RNN layer would have a shape of (num_timesteps, num_features)
, i.e. each sample consists of num_timesteps
timesteps where each timestep is a vector of length num_features
. Further, the number of timesteps (i.e. num_timesteps
) could be variable or unknown (i.e. None
) but the number of features (i.e. num_features
) should be fixed and specified from the beginning. Therefore, you need to change the shape of Input layer to be consistent with the RNN layer. For example:
inputs = keras.Input(shape=(None, 3)) # variable number of timesteps each with length 3
inputs = keras.Input(shape=(4, 3)) # 4 timesteps each with length 3
inputs = keras.Input(shape=(4, None)) # this is WRONG! you can't do this. Number of features must be fixed
Then, you also need to change the shape of input data (i.e. data
) as well to be consistent with the input shape you have specified (i.e. it must have a shape of (num_samples, num_timesteps, num_features)
).
As a side note, you could define the RNN layer more simply by using the SimpleRNN
layer directly:
label = keras.layers.SimpleRNN(units=5, activation='softmax')(inputs)
Thanks for your answer!
– david
Nov 27 '18 at 9:55
add a comment |
The input to a RNN layer would have a shape of (num_timesteps, num_features)
, i.e. each sample consists of num_timesteps
timesteps where each timestep is a vector of length num_features
. Further, the number of timesteps (i.e. num_timesteps
) could be variable or unknown (i.e. None
) but the number of features (i.e. num_features
) should be fixed and specified from the beginning. Therefore, you need to change the shape of Input layer to be consistent with the RNN layer. For example:
inputs = keras.Input(shape=(None, 3)) # variable number of timesteps each with length 3
inputs = keras.Input(shape=(4, 3)) # 4 timesteps each with length 3
inputs = keras.Input(shape=(4, None)) # this is WRONG! you can't do this. Number of features must be fixed
Then, you also need to change the shape of input data (i.e. data
) as well to be consistent with the input shape you have specified (i.e. it must have a shape of (num_samples, num_timesteps, num_features)
).
As a side note, you could define the RNN layer more simply by using the SimpleRNN
layer directly:
label = keras.layers.SimpleRNN(units=5, activation='softmax')(inputs)
The input to a RNN layer would have a shape of (num_timesteps, num_features)
, i.e. each sample consists of num_timesteps
timesteps where each timestep is a vector of length num_features
. Further, the number of timesteps (i.e. num_timesteps
) could be variable or unknown (i.e. None
) but the number of features (i.e. num_features
) should be fixed and specified from the beginning. Therefore, you need to change the shape of Input layer to be consistent with the RNN layer. For example:
inputs = keras.Input(shape=(None, 3)) # variable number of timesteps each with length 3
inputs = keras.Input(shape=(4, 3)) # 4 timesteps each with length 3
inputs = keras.Input(shape=(4, None)) # this is WRONG! you can't do this. Number of features must be fixed
Then, you also need to change the shape of input data (i.e. data
) as well to be consistent with the input shape you have specified (i.e. it must have a shape of (num_samples, num_timesteps, num_features)
).
As a side note, you could define the RNN layer more simply by using the SimpleRNN
layer directly:
label = keras.layers.SimpleRNN(units=5, activation='softmax')(inputs)
answered Nov 27 '18 at 9:46
todaytoday
11k22037
11k22037
Thanks for your answer!
– david
Nov 27 '18 at 9:55
add a comment |
Thanks for your answer!
– david
Nov 27 '18 at 9:55
Thanks for your answer!
– david
Nov 27 '18 at 9:55
Thanks for your answer!
– david
Nov 27 '18 at 9:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53496095%2fkeras-reports-typeerror-unsupported-operand-types-for-nonetype-and-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown