Python Keras:渐变有问题,自定义层赢了';在序列模型中不起作用

Python Keras:渐变有问题,自定义层赢了';在序列模型中不起作用,python,tensorflow,keras-layer,tf.keras,Python,Tensorflow,Keras Layer,Tf.keras,这是我收到的错误消息,代码如下: ValueError:没有为任何变量['variable:0']提供渐变。 在model.fit()中完成整个层的build()之后 它在执行build()之后和引发错误之前打印输入和标量,但张量均为空: Tensor("IteratorGetNext:0", shape=(None, 1), dtype=float32) <tf.Variable 'Variable:0' shape=(1,) dtype=float> 下面是模型的代码: 将

这是我收到的错误消息,代码如下:
ValueError:没有为任何变量['variable:0']提供渐变。
在model.fit()中完成整个层的build()之后

它在执行build()之后和引发错误之前打印输入和标量,但张量均为空:

Tensor("IteratorGetNext:0", shape=(None, 1), dtype=float32)  
<tf.Variable 'Variable:0' shape=(1,) dtype=float>
下面是模型的代码:

将numpy导入为np
导入tensorflow作为tf
导入操作系统
从tensorflow.keras导入顺序
从my_basic_图层导入MyBasicLayer
从tensorflow.keras.optimizers导入Adam
从tensorflow.keras.utils导入到
从tensorflow.python.keras.layers导入激活
从tensorflow.keras导入激活
k=2。
#加载数据集
inset=np.array([[i]表示范围(40)]中的i,dtype='float32')
开始=插入*k
#开始=分类(开始,数量=256)
#定义模型
模型=顺序()
model.add(MyBasicLayer(input_-shape=(1,)))#input_-shape=(4,)
#添加模型(激活(activations.softmax))
#编译模型
model.compile()
#符合模型
模型拟合(插图,开头)
model.summary()
可能与我所知道的有关:
我想在编译之前拥有一个model.summary(),但我得到了
此模型尚未建立。首先通过调用
Build()
或使用一些数据调用
fit()
来构建模型,或者在第一层中指定一个
input\u shape
参数以进行自动构建。

即使在第一层中添加el famoso
input_shape
参数


感谢您

在此处指定解决方案(答案部分),即使该解决方案出现在评论部分中,也是为了社区的利益

错误,
ValueError:没有为任何变量提供梯度:['variable:0']。
在上述情况下是因为在编译
模型时没有提供损失函数

那么,替换

model.compile()

将修复错误

为完整起见,使用
自定义层的简单工作示例代码如下所示:

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
上述代码的输出如下所示:

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
Tensor(“IteratorGetNext:0”,shape=(无,1),dtype=float32)
张量(“IteratorGetNext:0”,shape=(无,1),dtype=float32)
2/2[====================================]-0s 2ms/步-损耗:nan
型号:“顺序_3”
_________________________________________________________________
层(类型)输出形状参数
=================================================================
my_basic_layer_3(MyBasicLay倍数1
=================================================================
总参数:1
可培训参数:1
不可训练参数:0

希望这有帮助。快乐学习!

在这里指定解决方案(答案部分),即使它出现在评论部分,为了社区的利益

错误,
ValueError:没有为任何变量提供梯度:['variable:0']。
在上述情况下是因为在编译
模型时没有提供损失函数

那么,替换

model.compile()

将修复错误

为完整起见,使用
自定义层的简单工作示例代码如下所示:

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
上述代码的输出如下所示:

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
Tensor(“IteratorGetNext:0”,shape=(无,1),dtype=float32)
张量(“IteratorGetNext:0”,shape=(无,1),dtype=float32)
2/2[====================================]-0s 2ms/步-损耗:nan
型号:“顺序_3”
_________________________________________________________________
层(类型)输出形状参数
=================================================================
my_basic_layer_3(MyBasicLay倍数1
=================================================================
总参数:1
可培训参数:1
不可训练参数:0

希望这有帮助。学习愉快!

你不能有整数权重,渐变只为连续变量定义。@MatiasValdenegro谢谢,我还有一次w是浮点,尽管:ValueError:没有为任何变量提供渐变:['variable:0']。你知道这个github线程吗:?不客气。它解决了你的问题吗?它(部分)解释了当我在
model.compile()中添加
loss='classifical\u crossentropy'
作为参数时,为什么我的代码可以工作
!当我看到你的评论时,我正要编辑我的帖子。我不知道如何结束这个问题,顺便说一句,我该怎么办?你不能有整数权重,梯度只为连续变量定义。@MatiasValdenegro谢谢,我还有一次w是一个浮点值,尽管:ValueError:没有为任何变量提供梯度:['variable:0']。你知道这个github线程吗:?不客气。它解决了你的问题吗?它(部分)解释了当我在
model.compile()
中添加
loss='classifical\u crossentropy'
作为参数时,为什么我的代码可以工作。我刚要编辑我的帖子,突然看到你的评论。顺便说一句,我不知道如何结束这个问题,我该怎么办?