我一直沿用Text Generators这个模型,但被困住了。这是我第一次尝试tensorflow,所以我不确定问题是什么,但是由于它在本教程中有效并且对我不起作用,因此似乎可能已有更新。 model.add的格式是否已更改?我不知道如何解决此问题,因为我是图书馆的新手。
model = Sequential()
model.add(LSTM(400, input_shape=(X_modified.shape[1], X_modified.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(400))
model.add(Dropout(0.2))
model.add(Dense(Y_modified.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
引发错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-75-5a0fde131e03> in <module>()
1 model = Sequential()
----> 2 model.add(LSTM(400, input_shape=(X_modified.shape[1], X_modified.shape[2]), return_sequences=True))
3 model.add(Dropout(0.2))
4 model.add(LSTM(400))
5 model.add(Dropout(0.2))
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/keras/engine/sequential.py in add(self, layer)
162 # and create the node connecting the current layer
163 # to the input layer we just created.
--> 164 layer(x)
165 set_inputs = True
166 else:
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/keras/layers/recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
498
499 if initial_state is None and constants is None:
--> 500 return super(RNN, self).__call__(inputs, **kwargs)
501
502 # If any of `initial_state` or `constants` are specified and are Keras
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
429 'You can build it manually via: '
430 '`layer.build(batch_input_shape)`')
--> 431 self.build(unpack_singleton(input_shapes))
432 self.built = True
433
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/keras/layers/recurrent.py in build(self, input_shape)
459 self.cell.build([step_input_shape] + constants_shape)
460 else:
--> 461 self.cell.build(step_input_shape)
462
463 # set or validate state_spec
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/keras/layers/recurrent.py in build(self, input_shape)
1819 initializer=bias_initializer,
1820 regularizer=self.bias_regularizer,
-> 1821 constraint=self.bias_constraint)
1822 else:
1823 self.bias = None
~/anaconda/envs/dataweekends/lib/python3.7/site- packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/keras/engine/base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
247 if dtype is None:
248 dtype = K.floatx()
--> 249 weight = K.variable(initializer(shape),
250 dtype=dtype,
251 name=name,
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/keras/layers/recurrent.py in bias_initializer(_, *args, **kwargs)
1811 self.bias_initializer((self.units,), *args, **kwargs),
1812 initializers.Ones()((self.units,), *args, **kwargs),
-> 1813 self.bias_initializer((self.units * 2,), *args, **kwargs),
1814 ])
1815 else:
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in concatenate(tensors, axis)
1932 return tf.sparse_concat(axis, tensors)
1933 else:
-> 1934 return tf.concat([to_dense(x) for x in tensors], axis)
1935
1936
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in concat(concat_dim, values, name)
1073 ops.convert_to_tensor(concat_dim,
1074 name="concat_dim",
-> 1075 dtype=dtypes.int32).get_shape(
1076 ).assert_is_compatible_with(tensor_shape.scalar())
1077 return identity(values[0], name=scope)
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
667
668 if ret is None:
--> 669 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
670
671 if ret is NotImplemented:
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
174 as_ref=False):
175 _ = as_ref
--> 176 return constant(v, dtype=dtype, name=name)
177
178
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
163 tensor_value = attr_value_pb2.AttrValue()
164 tensor_value.tensor.CopyFrom(
--> 165 tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
166 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
167 const_tensor = g.create_op(
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
365 nparray = np.empty(shape, dtype=np_dt)
366 else:
--> 367 _AssertCompatible(values, dtype)
368 nparray = np.array(values, dtype=np_dt)
369 # check to them.
~/anaconda/envs/dataweekends/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py in _AssertCompatible(values, dtype)
300 else:
301 raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 302 (dtype.name, repr(mismatch), type(mismatch).__name__))
303
304
TypeError: Expected int32, got list containing Tensors of type '_Message' instead.
我曾尝试在Anaconda虚拟环境bash中更新tensorflow,但它说它已经升级:
mysite heathercohen$ pip install tensorflow --upgrade
Requirement already up-to-date: tensorflow in /usr/local/lib/python3.6/site-packages (1.10.1)
Requirement already satisfied, skipping upgrade: six>=1.10.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.11.0)
Requirement already satisfied, skipping upgrade: gast>=0.2.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (0.2.0)
Requirement already satisfied, skipping upgrade: absl-py>=0.1.6 in /usr/local/lib/python3.6/site-packages (from tensorflow) (0.4.0)
Requirement already satisfied, skipping upgrade: setuptools<=39.1.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (39.1.0)
Requirement already satisfied, skipping upgrade: grpcio>=1.8.6 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.14.1)
Requirement already satisfied, skipping upgrade: protobuf>=3.6.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (3.6.1)
Requirement already satisfied, skipping upgrade: astor>=0.6.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (0.7.1)
Requirement already satisfied, skipping upgrade: numpy<=1.14.5,>=1.13.3 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.14.5)
Requirement already satisfied, skipping upgrade: wheel>=0.26 in /usr/local/lib/python3.6/site-packages (from tensorflow) (0.31.1)
Requirement already satisfied, skipping upgrade: termcolor>=1.1.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.1.0)
Requirement already satisfied, skipping upgrade: tensorboard<1.11.0,>=1.10.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.10.0)
Requirement already satisfied, skipping upgrade: markdown>=2.6.8 in /usr/local/lib/python3.6/site-packages (from tensorboard<1.11.0,>=1.10.0->tensorflow) (2.6.11)
Requirement already satisfied, skipping upgrade: werkzeug>=0.11.10 in /usr/local/lib/python3.6/site-packages (from tensorboard<1.11.0,>=1.10.0->tensorflow) (0.14.1)
当我在同一环境中的jupyter笔记本中调用库时,我得到
TensorFlow version: 0.12.0
我还尝试过直接从jupyter笔记本升级它:
import sys
!{sys.executable} -m pip install --upgrade tensorflow
Requirement already up-to-date: tensorflow in /Users/heathercohen/anaconda/envs/dataweekends/lib/python3.7/site-packages (0.12.0)
Requirement already satisfied, skipping upgrade: protobuf==3.1.0 in /Users/heathercohen/anaconda/envs/dataweekends/lib/python3.7/site-packages (from tensorflow) (3.1.0)
Requirement already satisfied, skipping upgrade: wheel>=0.26 in /Users/heathercohen/anaconda/envs/dataweekends/lib/python3.7/site-packages (from tensorflow) (0.31.1)
Requirement already satisfied, skipping upgrade: numpy>=1.11.0 in /Users/heathercohen/anaconda/envs/dataweekends/lib/python3.7/site-packages (from tensorflow) (1.15.0)
Requirement already satisfied, skipping upgrade: six>=1.10.0 in /Users/heathercohen/anaconda/envs/dataweekends/lib/python3.7/site-packages (from tensorflow) (1.11.0)
Requirement already satisfied, skipping upgrade: setuptools in /Users/heathercohen/anaconda/envs/dataweekends/lib/python3.7/site-packages (from protobuf==3.1.0->tensorflow) (40.0.0)
任何人都可以确认该问题是否确实是tensorflow的版本,如果是,当上述尝试失败时如何更新它?
更新:我设法通过pip3安装了tensorflow 1.4.1,现在在上面的代码段之后出现以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-3e683776aec4> in <module>()
1 model = Sequential()
----> 2 model.add(LSTM(400, input_shape=(X_modified.shape[1], X_modified.shape[2]), return_sequences=True))
3 model.add(Dropout(0.2))
4 model.add(LSTM(400))
5 model.add(Dropout(0.2))
/usr/local/lib/python3.6/site-packages/keras/engine/sequential.py in add(self, layer)
162 # and create the node connecting the current layer
163 # to the input layer we just created.
--> 164 layer(x)
165 set_inputs = True
166 else:
/usr/local/lib/python3.6/site-packages/keras/layers/recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
498
499 if initial_state is None and constants is None:
--> 500 return super(RNN, self).__call__(inputs, **kwargs)
501
502 # If any of `initial_state` or `constants` are specified and are Keras
/usr/local/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
455 # Actually call the layer,
456 # collecting output(s), mask(s), and shape(s).
--> 457 output = self.call(inputs, **kwargs)
458 output_mask = self.compute_mask(inputs, previous_mask)
459
/usr/local/lib/python3.6/site-packages/keras/layers/recurrent.py in call(self, inputs, mask, training, initial_state)
2110 mask=mask,
2111 training=training,
-> 2112 initial_state=initial_state)
2113
2114 @property
/usr/local/lib/python3.6/site-packages/keras/layers/recurrent.py in call(self, inputs, mask, training, initial_state, constants)
607 mask=mask,
608 unroll=self.unroll,
--> 609 input_length=timesteps)
610 if self.stateful:
611 updates = []
/usr/local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in rnn(step_function, inputs, initial_states, go_backwards, mask, constants, unroll, input_length)
2960 parallel_iterations=32,
2961 swap_memory=True,
-> 2962 maximum_iterations=input_length)
2963 last_time = final_outputs[0]
2964 output_ta = final_outputs[1]
TypeError: while_loop() got an unexpected keyword argument 'maximum_iterations'
答案 0 :(得分:0)
通过卸载keras并在强制使用必要版本的同时重新安装来解决
pip3 uninstall keras
pip3 install keras==2.1.2