Keras:渐变问题,自定义层在顺序模型中不起作用
Keras : problem with gradients, custom layer won't work in a sequential model
这是我通过下面的代码得到的错误消息:
ValueError:没有为任何变量提供梯度:['Variable:0'].
在它通过整个层的 build() 之后,在 model.fit().
它在通过 build() 之后和引发错误之前打印输入和标量,但是张量都是空的:
Tensor("IteratorGetNext:0", shape=(None, 1), dtype=float32)
<tf.Variable 'Variable:0' shape=(1,) dtype=float>
我的目标是编写一个(基本)自定义层并将其插入(基本)模型中。
我的自定义图层可以自行正常工作,但我无法让模型适合。
该层采用张量并将其乘以标量。我希望我的模型给我输入*(我早先选择的标量)。
到目前为止,我已经收到了很多关于各种张量的 dtype 的错误警告(我使用的是 int32 而不是 float32),所以我写了很多演员表,而且我有一个更复杂的模型,但我把它剥离到了骨头里调试(它没有太大帮助......)。
我尝试使用和不使用 "build()",在标签上使用和不使用 "to_categorical",使用矢量输入和标量输入,以及其他可能无关紧要的东西。
图层代码如下:
from tensorflow.python.keras import layers
import tensorflow as tf
from tensorflow.python.ops import math_ops
from tensorflow.python.framework import tensor_shape
import h5py
import numpy as np
class MyBasicLayer(layers.Layer):
def __init__(self, **kwargs):
super().__init__(self)
self._set_dtype_policy('float32')
self.w = self.add_weight(shape=(1,), initializer='zeros', trainable=True)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape)
if tensor_shape.dimension_value(input_shape[-1]) is None:
raise ValueError('The last dimension of the inputs to `MyBasicLayer` should be defined. Found `None`.')
super().build(input_shape)
def call(self, inputs):
print(inputs)
print(self.w)
return tf.math.multiply(tf.dtypes.cast(inputs,dtype='float32'),self.w)
这里是模型的代码:
import numpy as np
import tensorflow as tf
import os
from tensorflow.keras import Sequential
from my_basic_layer import MyBasicLayer
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from tensorflow.python.keras.layers import Activation
from tensorflow.keras import activations
k = 2.
# load the dataset
inset = np.array([[i] for i in range(40)], dtype='float32')
outset = inset * k
#outset = to_categorical(outset, num_classes =256)
# define the model
model = Sequential()
model.add(MyBasicLayer(input_shape=(1,))) #input_shape=(4,)
#model.add(Activation(activations.softmax))
# compile the model
model.compile()
# fit the model
model.fit(inset, outset)
model.summary()
也许与我所知道的有关:
我想在编译前有一个 model.summary() 但我得到了
此模型尚未构建。首先通过调用 build()
或使用一些数据调用 fit()
来构建模型,或者在第一层指定一个 input_shape
参数以进行自动构建。
即使在第一层添加 el famoso input_shape
参数后。
谢谢
为了社区的利益。
,即使它出现在评论部分中,也请在此处(回答部分)指定解决方案
上述情况下的错误,ValueError: No gradients provided for any variable: ['Variable:0'].
是因为没有提供损失函数,而Model
是Compiled
。
所以,替换
model.compile()
和
model.compile(loss='categorical_crossentropy')
将修复错误。
为了完整起见,使用Custom Layer
的简单工作示例代码如下所示:
from tensorflow.python.keras import layers
import tensorflow as tf
from tensorflow.python.ops import math_ops
from tensorflow.python.framework import tensor_shape
import h5py
import numpy as np
class MyBasicLayer(layers.Layer):
def __init__(self, **kwargs):
super().__init__(self)
self._set_dtype_policy('float32')
self.w = self.add_weight(shape=(1,), initializer='zeros', trainable=True)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape)
if tensor_shape.dimension_value(input_shape[-1]) is None:
raise ValueError('The last dimension of the inputs to `MyBasicLayer` should be defined. Found `None`.')
super().build(input_shape)
def call(self, inputs):
print(inputs)
print(self.w)
return tf.math.multiply(tf.dtypes.cast(inputs,dtype='float32'),self.w)
import numpy as np
import tensorflow as tf
import os
from tensorflow.keras import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from tensorflow.python.keras.layers import Activation
from tensorflow.keras import activations
k = 2.
# load the dataset
inset = np.array([[i] for i in range(40)], dtype='float32')
outset = inset * k
#outset = to_categorical(outset, num_classes =256)
# define the model
model = Sequential()
model.add(MyBasicLayer(input_shape=(1,))) #input_shape=(4,)
# compile the model
model.compile(loss='categorical_crossentropy')
# fit the model
model.fit(inset, outset)
model.summary()
以上代码的输出如下所示:
Tensor("IteratorGetNext:0", shape=(None, 1), dtype=float32)
<tf.Variable 'Variable:0' shape=(1,) dtype=float32>
Tensor("IteratorGetNext:0", shape=(None, 1), dtype=float32)
<tf.Variable 'Variable:0' shape=(1,) dtype=float32>
2/2 [==============================] - 0s 2ms/step - loss: nan
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
my_basic_layer_3 (MyBasicLay multiple 1
=================================================================
Total params: 1
Trainable params: 1
Non-trainable params: 0
希望这对您有所帮助。快乐学习!
这是我通过下面的代码得到的错误消息:
ValueError:没有为任何变量提供梯度:['Variable:0'].
在它通过整个层的 build() 之后,在 model.fit().
它在通过 build() 之后和引发错误之前打印输入和标量,但是张量都是空的:
Tensor("IteratorGetNext:0", shape=(None, 1), dtype=float32)
<tf.Variable 'Variable:0' shape=(1,) dtype=float>
我的目标是编写一个(基本)自定义层并将其插入(基本)模型中。 我的自定义图层可以自行正常工作,但我无法让模型适合。 该层采用张量并将其乘以标量。我希望我的模型给我输入*(我早先选择的标量)。
到目前为止,我已经收到了很多关于各种张量的 dtype 的错误警告(我使用的是 int32 而不是 float32),所以我写了很多演员表,而且我有一个更复杂的模型,但我把它剥离到了骨头里调试(它没有太大帮助......)。
我尝试使用和不使用 "build()",在标签上使用和不使用 "to_categorical",使用矢量输入和标量输入,以及其他可能无关紧要的东西。
图层代码如下:
from tensorflow.python.keras import layers
import tensorflow as tf
from tensorflow.python.ops import math_ops
from tensorflow.python.framework import tensor_shape
import h5py
import numpy as np
class MyBasicLayer(layers.Layer):
def __init__(self, **kwargs):
super().__init__(self)
self._set_dtype_policy('float32')
self.w = self.add_weight(shape=(1,), initializer='zeros', trainable=True)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape)
if tensor_shape.dimension_value(input_shape[-1]) is None:
raise ValueError('The last dimension of the inputs to `MyBasicLayer` should be defined. Found `None`.')
super().build(input_shape)
def call(self, inputs):
print(inputs)
print(self.w)
return tf.math.multiply(tf.dtypes.cast(inputs,dtype='float32'),self.w)
这里是模型的代码:
import numpy as np
import tensorflow as tf
import os
from tensorflow.keras import Sequential
from my_basic_layer import MyBasicLayer
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from tensorflow.python.keras.layers import Activation
from tensorflow.keras import activations
k = 2.
# load the dataset
inset = np.array([[i] for i in range(40)], dtype='float32')
outset = inset * k
#outset = to_categorical(outset, num_classes =256)
# define the model
model = Sequential()
model.add(MyBasicLayer(input_shape=(1,))) #input_shape=(4,)
#model.add(Activation(activations.softmax))
# compile the model
model.compile()
# fit the model
model.fit(inset, outset)
model.summary()
也许与我所知道的有关:
我想在编译前有一个 model.summary() 但我得到了
此模型尚未构建。首先通过调用 build()
或使用一些数据调用 fit()
来构建模型,或者在第一层指定一个 input_shape
参数以进行自动构建。
即使在第一层添加 el famoso input_shape
参数后。
谢谢
为了社区的利益。
,即使它出现在评论部分中,也请在此处(回答部分)指定解决方案上述情况下的错误,ValueError: No gradients provided for any variable: ['Variable:0'].
是因为没有提供损失函数,而Model
是Compiled
。
所以,替换
model.compile()
和
model.compile(loss='categorical_crossentropy')
将修复错误。
为了完整起见,使用Custom Layer
的简单工作示例代码如下所示:
from tensorflow.python.keras import layers
import tensorflow as tf
from tensorflow.python.ops import math_ops
from tensorflow.python.framework import tensor_shape
import h5py
import numpy as np
class MyBasicLayer(layers.Layer):
def __init__(self, **kwargs):
super().__init__(self)
self._set_dtype_policy('float32')
self.w = self.add_weight(shape=(1,), initializer='zeros', trainable=True)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape)
if tensor_shape.dimension_value(input_shape[-1]) is None:
raise ValueError('The last dimension of the inputs to `MyBasicLayer` should be defined. Found `None`.')
super().build(input_shape)
def call(self, inputs):
print(inputs)
print(self.w)
return tf.math.multiply(tf.dtypes.cast(inputs,dtype='float32'),self.w)
import numpy as np
import tensorflow as tf
import os
from tensorflow.keras import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from tensorflow.python.keras.layers import Activation
from tensorflow.keras import activations
k = 2.
# load the dataset
inset = np.array([[i] for i in range(40)], dtype='float32')
outset = inset * k
#outset = to_categorical(outset, num_classes =256)
# define the model
model = Sequential()
model.add(MyBasicLayer(input_shape=(1,))) #input_shape=(4,)
# compile the model
model.compile(loss='categorical_crossentropy')
# fit the model
model.fit(inset, outset)
model.summary()
以上代码的输出如下所示:
Tensor("IteratorGetNext:0", shape=(None, 1), dtype=float32)
<tf.Variable 'Variable:0' shape=(1,) dtype=float32>
Tensor("IteratorGetNext:0", shape=(None, 1), dtype=float32)
<tf.Variable 'Variable:0' shape=(1,) dtype=float32>
2/2 [==============================] - 0s 2ms/step - loss: nan
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
my_basic_layer_3 (MyBasicLay multiple 1
=================================================================
Total params: 1
Trainable params: 1
Non-trainable params: 0
希望这对您有所帮助。快乐学习!