【问题标题】:Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor尝试将具有不受支持的类型 (<class 'NoneType'>) 的值 (None) 转换为张量
【发布时间】:2022-04-12 22:58:25
【问题描述】:

我遇到了错误

Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor

使用非常简单的代码,无法确定错误来自何处。在 Jupyter notebook 中运行相同的代码是可行的。

import tensorflow as tf
import numpy as np

# inherit from this base class
from tensorflow.keras.layers import Layer

class SimpleDense(Layer):

    def __init__(self, units=32):
        '''Initializes the instance attributes'''
        super(SimpleDense, self).__init__()
        self.units = units

    def build(self, input_shape):
        '''Create the state of the layer (weights)'''
        # initialize the weights
        w_init = tf.random_normal_initializer()
        self.w = tf.Variable(name="kernel",
            initial_value=w_init(shape=(input_shape[-1], self.units),
                                 dtype='float32'),
            trainable=True)

        # initialize the biases
        b_init = tf.zeros_initializer()
        self.b = tf.Variable(name="bias",
            initial_value=b_init(shape=(self.units,), dtype='float32'),
            trainable=True)

    def call(self, inputs):
        '''Defines the computation from inputs to outputs'''
        return tf.matmul(inputs, self.w) + self.b

# declare an instance of the class
my_dense = SimpleDense(units=1)

# define an input and feed into the layer
x = tf.ones((2, 1))
y = my_dense(x)

# parameters of the base Layer class like `variables` can be used
print(my_dense.variables)

# define the dataset
xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)


# use the Sequential API to build a model with our custom layer
my_layer = SimpleDense(units=1)
model = tf.keras.Sequential([my_layer])

# configure and train the model
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(xs, ys, epochs=500,verbose=0)

# perform inference
print(model.predict([10.0]))

# see the updated state of the variables
print(my_layer.variables)

更新:包含回溯的错误消息

In user code:

    File "keras\engine\training.py", line 878, in train_function  *
        return step_function(self, iterator)
    File "keras\engine\training.py", line 867, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "keras\engine\training.py", line 860, in run_step  **
        outputs = model.train_step(data)
    File "keras\engine\training.py", line 808, in train_step
        y_pred = self(x, training=True)
    File "keras\utils\traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "Dense_Layer.py", line 19, in build
        initial_value=w_init(shape=(input_shape[-1], self.units),

    ValueError: Exception encountered when calling layer "sequential" (type Sequential).
    
    Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.
    
    Call arguments received:
      • inputs=tf.Tensor(shape=(None,), dtype=float32)
      • training=True
      • mask=None
  File "Dense_Layer.py", line 19, in build
    initial_value=w_init(shape=(input_shape[-1], self.units),

During handling of the above exception, another exception occurred:

  File "Temp\__autograph_generated_filexvnq5chx.py", line 15, in tf__train_function
    retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
  File "Dense_Layer.py", line 19, in build
    initial_value=w_init(shape=(input_shape[-1], self.units),

During handling of the above exception, another exception occurred:

  File "Dense_Layer.py", line 54, in <module>
    model.fit(xs, ys, epochs=500,verbose=0)

Python 版本: Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32

版权所有:Deeplearning.ai

【问题讨论】:

  • 发布整个错误消息,包括回溯...
  • 这个错误是由于xs的形状是(6,),因此模型的input_shapeNone,所以input_shape[-1]不起作用。如果添加xs = xs.reshape(-1, 1),则xs 的形状变为(6,1),模型的input_shape 变为(None, 1),因此input_shape[-1] 返回1,代码按预期工作。
  • 非常感谢!!这绝对是我应该发现的错误。

标签: python tensorflow keras deep-learning neural-network


【解决方案1】:

向 Sequential API 添加“输入层”将解决此问题。

可能是不同 tensorflow 版本的问题。这可能是它在 Jupyter notebook 中工作的原因

变化:

# use the Sequential API to build a model with our custom layer
    input_layer = tf.keras.Input(shape=(1,)) # Added Newly
    my_layer = SimpleDense(units=1)
    model = tf.keras.Sequential([input_layer, my_layer]) # Added input_layer here.

完整代码:

import tensorflow as tf
import numpy as np

# inherit from this base class
from tensorflow.keras.layers import Layer

class SimpleDense(Layer):

    def __init__(self, units=32):
        '''Initializes the instance attributes'''
        super(SimpleDense, self).__init__()
        self.units = units

    def build(self, input_shape):
        '''Create the state of the layer (weights)'''
        # initialize the weights
        w_init = tf.random_normal_initializer()
        self.w = tf.Variable(name="kernel",
            initial_value=w_init(shape=(input_shape[-1], self.units),
                                 dtype='float32'),
            trainable=True)

        # initialize the biases
        b_init = tf.zeros_initializer()
        self.b = tf.Variable(name="bias",
            initial_value=b_init(shape=(self.units,), dtype='float32'),
            trainable=True)

    def call(self, inputs):
        '''Defines the computation from inputs to outputs'''
        return tf.matmul(inputs, self.w) + self.b

# declare an instance of the class
my_dense = SimpleDense(units=1)

# define an input and feed into the layer
x = tf.ones((2, 1))
y = my_dense(x)

# parameters of the base Layer class like `variables` can be used
print(my_dense.variables)

# define the dataset
xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)


# use the Sequential API to build a model with our custom layer
input_layer = tf.keras.Input(shape=(1,)) # Added Newly
my_layer = SimpleDense(units=1)
model = tf.keras.Sequential([input_layer, my_layer]) # Added input_layer here.

# configure and train the model
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(xs, ys, epochs=500,verbose=0)

# perform inference
print(model.predict([10.0]))

# see the updated state of the variables
print(my_layer.variables)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    相关资源
    最近更新 更多