tensorflow Tensor "Failed to convert elements of to Tensor" in losses = tf. convert_to_tensor(losses)in www.example.com()model.fit ()

zhte4eai  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(212)

我在尝试将我的模型与MNIST数字数据集相匹配时遇到了问题。作为参考,这是通过VS Code在Jupyter Notebook中一次运行一个代码块,使用tensorflow版本2.11.1的解释器
试图打电话

hist = model.fit(x_train[:30, :, :], y_train[:30, :], batch_size = 1)

我得到以下信息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[307], line 1
----> 1 hist = model.fit(x_train[:30, :, :], y_train[:30, :], batch_size = 1)

File ~/anaconda3/envs/digitrecogniser/lib/python3.10/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File /tmp/__autograph_generated_filewfoheau0.py:15, in outer_factory..inner_factory..tf__train_function(iterator)
     13 try:
     14     do_return = True
---> 15     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16 except:
     17     do_return = False

TypeError: in user code:

    File "/home/diogo/anaconda3/envs/digitrecogniser/lib/python3.10/site-packages/keras/engine/training.py", line 1249, in train_function  *
        return step_function(self, iterator)
    File "/home/diogo/anaconda3/envs/digitrecogniser/lib/python3.10/site-packages/keras/engine/training.py", line 1233, in step_function  **
...
    File "/home/diogo/anaconda3/envs/digitrecogniser/lib/python3.10/site-packages/keras/utils/losses_utils.py", line 328, in compute_weighted_loss
        losses = tf.convert_to_tensor(losses)

    TypeError: Failed to convert elements of  to Tensor. Consider casting elements to a supported type.

下面是相关代码的其余部分,包括预处理,以防出现问题。谢谢!

def loadMNIST():
    # Use keras' inbuilt datasets module to load data
    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
    
    # x_train.shape = (60000, 28, 28)
    # y_train.shape = (60000, )
    # x_test.shape = (10000, 28, 28)
    # y_test.shape = (10000, )

    # Split 50:10 on train:validation, i.e. 50,000 datapoints for train, 10,000 for validation
    x_val, y_val = x_train[-10_000:, :, :], y_train[-10_000:]
    x_train, y_train = x_train[:-10_000, :, :], y_train[:-10_000]

    return (x_train, y_train), (x_val, y_val), (x_test, y_test)

(x_train, y_train), (x_val, y_val), (x_test, y_test) = loadMNIST()

def preprocess(data):
    x_train, y_train, x_val, y_val, x_test, y_test = data

    x_train = tf.convert_to_tensor(x_train, dtype = tf.float32) / 255
    x_val   = tf.convert_to_tensor(x_val, dtype = tf.float32) / 255
    x_test  = tf.convert_to_tensor(x_test, dtype = tf.float32) / 255

    y_train = tf.one_hot(tf.convert_to_tensor(y_train), depth = 10)
    y_val   = tf.one_hot(tf.convert_to_tensor(y_val),  depth = 10)
    y_test  = tf.one_hot(tf.convert_to_tensor(y_test), depth = 10)

    return x_train, y_train, x_val, y_val, x_test, y_test

x_train, y_train, x_val, y_val, x_test, y_test = preprocess((x_train, y_train, x_val, y_val, x_test, y_test))

class CNN(tf.keras.Model):

    def __init__(self):
        super().__init__()
        self.conv1 = tf.keras.layers.Conv2D(filters = 3, kernel_size = 3, strides = (1, 1), padding = 'valid', activation = 'relu', input_shape = (None, 28, 28, 1))
        self.drop1 = tf.keras.layers.Dropout(rate = 0.5)
                
        self.conv2 = tf.keras.layers.Conv2D(filters = 3, kernel_size = 3, strides = (1, 1), padding = 'valid', activation = 'relu', input_shape = (None, 28, 28, 1))
        self.drop2 = tf.keras.layers.Dropout(rate = 0.5)

        self.flatten = tf.keras.layers.Flatten()

        self.dense1 = tf.keras.layers.Dense(units = 10, activation = 'relu')
        self.dense2 = tf.keras.layers.Dense(units = 10, activation = 'softmax')

    def call(self, inputs):
        x = tf.reshape(inputs, [-1, inputs.shape[1], inputs.shape[2], 1])
        
        x = self.conv1(x)
        x = self.drop1(x)

        x = self.conv2(x)
        x = self.drop2(x)
        
        x = self.flatten(x)

        x = self.dense1(x)
        x = self.dense2(x)

        return x
    
model = CNN()

model.compile(optimizer = 'Adam', loss = tf.keras.losses.CategoricalCrossentropy, metrics = ['accuracy', 'AUC'])

hist = model.fit(x_train[:30, :, :], y_train[:30, :], batch_size = 1)

我还在努力掌握TensorFlow的窍门,谢谢你的帮助。

mwngjboj

mwngjboj1#

CategoricalCrossentropy损失函数中缺少Parathensis (),这导致了错误。
请使用
loss = tf.keras.losses.CategoricalCrossentropy()
而不是
loss = tf.keras.losses.CategoricalCrossentropy
请检查此复制的代码要点供您参考。

相关问题