我正面临着错误
Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor
有一个非常琐碎的代码,不知道错误来自哪里。在木星笔记本上运行相同的代码是可行的。
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)
:Python3.9.5(tag/v3.9.5:0a7dcbd,2021年5月3日,17:27:52) MSC v.1928 64位(AMD64) on win32
CopyRight: Deeplearning.ai
发布于 2022-04-12 14:22:58
在顺序API中添加一个“输入层”将解决这个问题。
可能是不同的tensorflow版本的问题。这可能就是它在木星笔记本上工作的原因。
更改:
# 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)
https://stackoverflow.com/questions/70507803
复制相似问题