Hi, first of all thanks for great tutorial. I have learned a lot. But I have a problem with LSTM part. I am trying to run your code, but I still get an error.
`import tensorflow as tf
from tensorflow import keras
from keras.callbacks import TensorBoard
!rm -rf ./logs/
keras.backend.clear_session()
%load_ext tensorboard
model = Sequential()
Adding the input layer
model.add(LSTM(units=48, activation='tanh', kernel_initializer=tf.keras.initializers.glorot_uniform(seed=26), input_shape = (X_train.shape[1], 1)))
Adding the output layer
model.add(Dense(1, name="output_layer"))
Compiling the RNN
model.compile(optimizer = keras.optimizers.Adam(learning_rate=0.001), loss = root_mean_squared_error)
#Using Tensorboard
logdir = "logs"
tensorboard_callback = TensorBoard(log_dir=logdir, histogram_freq=5, write_graph=True)
Fitting the RNN to the Training set
model.fit(trainX, trainY, epochs = 50, batch_size = 16, validation_data = (cvX, cvY), callbacks = [tensorboard_callback])`
And this is the error:
ValueError Traceback (most recent call last)
in ()
10
11 # Adding the input layer
---> 12 model.add(LSTM(units=48, activation='tanh', kernel_initializer=tf.keras.initializers.glorot_uniform(seed=26), input_shape = (X_train.shape[1], 1)))
13
14 # Adding the output layer
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/keras/engine/sequential.py in add(self, layer)
164 # and create the node connecting the current layer
165 # to the input layer we just created.
--> 166 layer(x)
167 set_inputs = True
168 else:
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/keras/layers/recurrent.py in call(self, inputs, initial_state, constants, **kwargs)
539
540 if initial_state is None and constants is None:
--> 541 return super(RNN, self).call(inputs, **kwargs)
542
543 # If any of initial_state
or constants
are specified and are Keras
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs)
73 if _SYMBOLIC_SCOPE.value:
74 with get_graph().as_default():
---> 75 return func(*args, **kwargs)
76 else:
77 return func(*args, **kwargs)
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in call(self, inputs, **kwargs)
461 'You can build it manually via: '
462 'layer.build(batch_input_shape)
')
--> 463 self.build(unpack_singleton(input_shapes))
464 self.built = True
465
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/keras/layers/recurrent.py in build(self, input_shape)
500 self.cell.build([step_input_shape] + constants_shape)
501 else:
--> 502 self.cell.build(step_input_shape)
503
504 # set or validate state_spec
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/keras/layers/recurrent.py in build(self, input_shape)
1917 initializer=self.kernel_initializer,
1918 regularizer=self.kernel_regularizer,
-> 1919 constraint=self.kernel_constraint)
1920 self.recurrent_kernel = self.add_weight(
1921 shape=(self.units, self.units * 4),
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
280 dtype=dtype,
281 name=name,
--> 282 constraint=constraint)
283 if regularizer is not None:
284 with K.name_scope('weight_regularizer'):
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in variable(value, dtype, name, constraint)
618 """
619 v = tf_keras_backend.variable(
--> 620 value, dtype=dtype, name=name, constraint=constraint)
621 if hasattr(value, 'tocoo'):
622 v._keras_shape = value.tocoo().shape
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py in variable(value, dtype, name, constraint)
812 dtype=dtypes_module.as_dtype(dtype),
813 name=name,
--> 814 constraint=constraint)
815 if isinstance(value, np.ndarray):
816 v._keras_shape = value.shape
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py in call(cls, *args, **kwargs)
258 return cls._variable_v1_call(*args, **kwargs)
259 elif cls is Variable:
--> 260 return cls._variable_v2_call(*args, **kwargs)
261 else:
262 return super(VariableMetaclass, cls).call(*args, **kwargs)
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py in _variable_v2_call(cls, initial_value, trainable, validate_shape, caching_device, name, variable_def, dtype, import_scope, constraint, synchronization, aggregation, shape)
252 synchronization=synchronization,
253 aggregation=aggregation,
--> 254 shape=shape)
255
256 def call(cls, *args, **kwargs):
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py in (**kws)
233 shape=None):
234 """Call on Variable class. Useful to force the signature."""
--> 235 previous_getter = lambda **kws: default_variable_creator_v2(None, **kws)
236 for _, getter in ops.get_default_graph()._variable_creator_stack: # pylint: disable=protected-access
237 previous_getter = _make_getter(getter, previous_getter)
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/variable_scope.py in default_variable_creator_v2(next_creator, **kwargs)
2643 synchronization=synchronization,
2644 aggregation=aggregation,
-> 2645 shape=shape)
2646
2647
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py in call(cls, *args, **kwargs)
260 return cls._variable_v2_call(*args, **kwargs)
261 else:
--> 262 return super(VariableMetaclass, cls).call(*args, **kwargs)
263
264
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in init(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, variable_def, import_scope, constraint, distribute_strategy, synchronization, aggregation, shape)
1409 aggregation=aggregation,
1410 shape=shape,
-> 1411 distribute_strategy=distribute_strategy)
1412
1413 def _init_from_args(self,
/Users/jaroslavvomacka/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in _init_from_args(self, initial_value, trainable, collections, caching_device, name, dtype, constraint, synchronization, aggregation, distribute_strategy, shape)
1492 if isinstance(initial_value, ops.Tensor) and hasattr(
1493 initial_value, "graph") and initial_value.graph.building_function:
-> 1494 raise ValueError("Tensor-typed variable initializers must either be "
1495 "wrapped in an init_scope or callable "
1496 "(e.g., `tf.Variable(lambda : "
ValueError: Tensor-typed variable initializers must either be wrapped in an init_scope or callable (e.g., tf.Variable(lambda : tf.truncated_normal([10, 40]))
) when building functions. Please file a feature request if this restriction inconveniences you.
Do you have an idea where can be the problem? Thank you