I am trying to run some python tensorflow code on a debian 9.5 stretch system on google cloud. I am Using tensorflow GPU version of this (the latest version) with the approriate CODA and cuDNN software installed. Here is my code:
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train = tf.keras.utils.normalize(x_train, axis=1) # scales all values between 0 and 1 on pixel image
x_test = tf.keras.utils.normalize(x_test, axis=1)
model = tf.keras.models.Sequential()
model.add (tf.keras.layers.Flatten()) # flattens the 28x28 pixels into long flat vector
model.add(tf.layers.Dense(128, activation=tf.nn.relu))# builds hidden layer 128 neurons (we can tweak) and activation func - use this as default, sigmoid function
model.add(tf.layers.Dense(128, activation=tf.nn.relu)) # second layer
model.add(tf.layers.Dense(10, activation=tf.nn.softmax)) # output layer - number of output neurons =classes
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics = ['accuracy']) # adam is least squares test
model.fit(x_train, y_train, epochs=3)
#model.fit(x_train, y_train, epochs=3)
However, I get the following error:
AttributeError Traceback (most recent call last) <
ipython-input-1-3604b3cbf07d> in <module>
16 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics = ['accuracy']) # adam is least squares test
17
---> 18 model.fit(x_train, y_train, epochs=3)
19 #model.fit(x_train, y_train, epochs=3)
20
~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1507 steps_name='steps_per_epoch',
1508 steps=steps_per_epoch,
-> 1509 validation_split=validation_split)
1510
1511 # Prepare validation data.
~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split)
991 x, y = next_element
992 x, y, sample_weights = self._standardize_weights(x, y, sample_weight,
--> 993 class_weight, batch_size)
994 return x, y, sample_weights
995
~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _standardize_weights(self, x, y, sample_weight, class_weight, batch_size)
1027 if not self.inputs:
1028 is_build_called = True
-> 1029 self._set_inputs(x)
1030
1031 if y is not None:
~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py in _method_wrapper(self, *args, **kwargs)
424 self._setattr_tracking = False # pylint: disable=protected-access
425 try:
--> 426 method(self, *args, **kwargs)
427 finally:
428 self._setattr_tracking = previous_value # pylint: disable=protected-access
~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _set_inputs(self, inputs, training)
1220 else:
1221 input_shape = (None,) + inputs.shape[1:]
-> 1222 self.build(input_shape=input_shape)
1223 if context.executing_eagerly():
1224 self._eager_set_inputs(inputs)
~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py in build(self, input_shape)
219 for layer in self.layers:
220 if not layer.built:
--> 221 with ops.name_scope(layer._name_scope()):
222 layer.build(shape)
223 layer.built = True
~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/layers/base.py in _name_scope(self)
139 def _name_scope(self):
140 """Determines op naming for the Layer."""
--> 141 return self._current_scope.original_name_scope
142
143 def _set_scope(self, scope=None):
AttributeError: 'NoneType' object has no attribute 'original_name_scope'
This code works fine on my laptop using the CPU version of tensorflow, however I am getting this error on the virtual machine. Any ideas?
i had the similar issue and the solution is to use
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
instead of
model.add(tf.layers.Dense(128, activation=tf.nn.relu))
answer by @Josh will be required for saving the model.
I had the same issue, and resolved it by setting the input_size
parameter in the first layer of the sequential model.
this line:
model.add (tf.keras.layers.Flatten())
should be:
model.add (tf.keras.layers.Flatten(input_size=(28, 28))
来源:https://stackoverflow.com/questions/52690293/tensorflow-attributeerror-nonetype-object-has-no-attribute-original-name-sc