如何在keras 在自定义类中使用numpy不 报错
在使用numpy()时,需要确保输入的数据类型为numpy数组。如果输入的数据类型不是numpy数组,可以将其转换为numpy数组,如下所示:
import numpy as np
from tensorflow import keras
class MyModel(keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense = keras.layers.Dense(10)
def call(self, inputs):
x = np.array(inputs) # 将输入转换为numpy数组
x = self.dense(x)
return x
在上面的例子中,我们将输入数据转换为numpy数组,然后再使用numpy()方法,以避免报错。
原文地址: https://www.cveoy.top/t/topic/bKUK 著作权归作者所有。请勿转载和采集!