简体   繁体   English

Tensorflow:AttributeError:'NoneType'对象没有属性'original_name_scope'

[英]Tensorflow: AttributeError: 'NoneType' object has no attribute 'original_name_scope'

I am trying to run some python tensorflow code on a debian 9.5 stretch system on google cloud. 我正在尝试在Google Cloud的Debian 9.5拉伸系统上运行一些python tensorflow代码。 I am Using tensorflow GPU version of this (the latest version) with the approriate CODA and cuDNN software installed. 我正在使用此tensorflow GPU版本(最新版本)并安装了合适的CODA和cuDNN软件。 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) < AttributeError Traceback(最近一次通话最近)<

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. 这段代码在使用CPU版本的tensorflow的笔记本电脑上可以正常工作,但是在虚拟机上却出现此错误。 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. 保存模型需要@Josh回答。

I had the same issue, and resolved it by setting the input_size parameter in the first layer of the sequential model. 我遇到了同样的问题,并通过在顺序模型的第一层中设置了input_size参数来解决了该问题。

this line: model.add (tf.keras.layers.Flatten()) 这行: model.add (tf.keras.layers.Flatten())

should be: model.add (tf.keras.layers.Flatten(input_size=(28, 28)) 应该是: model.add (tf.keras.layers.Flatten(input_size=(28, 28))

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

相关问题 AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;name&#39;(与不同的 tensorflow 版本有关) - AttributeError: 'NoneType' object has no attribute 'name' ( related to different tensorflow versions ) Tensorflow VariableScope:original_name_scope与名称 - Tensorflow VariableScope: original_name_scope vs name Tensorflow AttributeError:&#39;NoneType&#39;对象没有属性&#39;keys&#39; - Tensorflow AttributeError: 'NoneType' object has no attribute 'keys' AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;name&#39;? - AttributeError: 'NoneType' object has no attribute 'name'? AttributeError:&#39;NoneType&#39;对象没有属性&#39;name - AttributeError: 'NoneType' object has no attribute 'name AttributeError: 'NoneType' object 没有带有 streamlit 的属性 'name' - AttributeError: 'NoneType' object has no attribute 'name' with streamlit AttributeError:&#39;NoneType&#39;对象没有属性&#39;name&#39; - AttributeError: 'NoneType' object has no attribute 'name' Flask AttributeError: 'NoneType' object 没有属性 'name' - Flask AttributeError: 'NoneType' object has no attribute 'name' Tensorflow AttributeError:'NoneType'对象没有属性'TF_DeleteStatus' - Tensorflow AttributeError: 'NoneType' object has no attribute 'TF_DeleteStatus' AttributeError: &#39;str&#39; 对象在 Tensorflow 中没有属性 &#39;name&#39; - AttributeError: 'str' object has no attribute 'name' in Tensorflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM