tensorflow 值错误:收到的调用参数:·输入=tf,Tensor(形状=(无,1),数据类型=浮点数32)·训练=无

xxhby3vn  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(203)

我在输入层中遇到了上述错误,但我似乎找不到问题所在。我正在处理一个文本分类数据集,想使用通用语句编码器模型进行嵌入,但似乎在这里不起作用。当我使用嵌入层和文本矢量化层创建自己的嵌入时,它完美地工作了。

use = hub.KerasLayer('https://tfhub.dev/google/universal-sentence-encoder/4',trainable=False,dtype=tf.string,input_shape=[])
    class CnnModel(keras.Model):
        def __init__(self,channels):
            super(CnnModel,self).__init__()
            self.conversion = keras.Sequential([
                Input(shape=(1,)),
                use
            ])
            self.computation = keras.Sequential([
                Conv1D(filters=channels,kernel_size=2,strides=1,padding='valid'),
                MaxPool1D(pool_size=2,strides=1,padding='valid'),
                Conv1D(filters=channels,kernel_size=2,strides=1,padding='same'),
            ])
            self.dense = keras.Sequential([
                GlobalMaxPooling1D(),
                Dense(units=1,activation='sigmoid')
            ])
        def call(self,input_tensor):
            print(input_tensor.shape)
            x = self.conversion(input_tensor)
            x = self.computation(x)
            x = self.dense(x)
            return x
    model = CnnModel(16)

我甚至不能示例化这个类并得到这个错误:

ValueError                                Traceback (most recent call last)
c:\Users\gupta\OneDrive\Desktop\GIT\Repo\rough.ipynb Cell 6 in <cell line: 25>()
     23         x = self.dense(x)
     24         return x
---> 25 model = CnnModel(16)

c:\Users\gupta\OneDrive\Desktop\GIT\Repo\rough.ipynb Cell 6 in CnnModel.__init__(self, channels)
      4 def __init__(self,channels):
      5     super(CnnModel,self).__init__()
----> 6     self.conversion = keras.Sequential([
      7         Input(shape=(1,)),
      8         use
      9     ])
     10     self.computation = keras.Sequential([
     11         Conv1D(filters=channels,kernel_size=2,strides=1,padding='valid'),
     12         MaxPool1D(pool_size=2,strides=1,padding='valid'),
     13         Conv1D(filters=channels,kernel_size=2,strides=1,padding='same'),
     14     ])
     15     self.dense = keras.Sequential([
     16         GlobalMaxPooling1D(),
     17         Dense(units=1,activation='sigmoid')
     18     ])

File c:\Users\gupta\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\training\tracking\base.py:629, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
...

Call arguments received:
  • inputs=tf.Tensor(shape=(None, 1), dtype=float32)
  • training=None

我还尝试使用Sequential API创建此模型,并设法将相同的错误本地化为:
(this也会产生完全相同的错误)

ann = keras.Sequential([
        Input(shape=(1,)),
        use
    ])
z9ju0rcb

z9ju0rcb1#

我试着为文本分类建立模型,它对我很有效。当我们处理文本数据时,在输入层提供空白形状和字符串数据类型对我很有效。

keras.Input(shape=[], dtype = tf.string)

示例代码片段:

use = hub.KerasLayer('https://tfhub.dev/google/universal-sentence-encoder/4',trainable=False,dtype=tf.string,input_shape=[])

# build model: Sequential
ann = keras.Sequential([
      keras.Input(shape=[], dtype = tf.string),
      use,
      keras.layers.Dense(1, activation = "sigmoid")
    ])

# compile model
ann.compile(Adam(2e-5), loss='binary_crossentropy', metrics=['accuracy'])
ann.summary()

#fit model
ann.fit(train_dataset, epochs=1,
                    validation_data=test_dataset,
                    validation_steps=3)

相关问题