gpt4 book ai didi

python - 预期类型错误 : numpy. ndarray 或 cuda.ndarray

转载 作者:行者123 更新时间:2023-11-30 09:03:54 27 4
gpt4 key购买 nike

我想在 chainer 中训练 Convolution3d 模型。在我的训练室中,我遇到了这个错误。

TypeError: numpy.ndarray or cuda.ndarray are expected.

我认为错误的原因是输入是一个列表。所以,我将输入数组更改为 numpy 数组。但我有同样的错误。

这是火车代码。

model = conv_3d()
model.to_gpu(0)


optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9)

optimizer.setup(model)

max_epoch = 100
batch_size = 50

epoch_idx = 0

while epoch_idx < max_epoch:

train_path = random.sample(train_path, len(train_path))


train_losses = []
for i in range(int(len(train_path) // batch_size)):



batch = train_path[i * batch_size: (i+1) * batch_size]
input_movie, target_movie = loader(batch)

prediction_train = model(input_movie)
loss = F.mean_squared_error(prediction_train,target_movie)

train_losses.append(to_cpu(loss.array))

model.cleargrads()
loss.backward()

optimizer.update()

print('epoch:{:03d} train_loss:{:.04f} '.format(epoch_idx + 1, np.mean(train_losses)), end='')

test_losses = []
for test_batch in range(len(validation)//batch_size):

batch = validation[test_batch * batch_size:(test_batch + 1) * 50]
validation_input_movie, validation_target_movie = loader(batch)

prediction_validation = model(validation_input_movie)

loss_validation = F.mean_squared_error(prediction_validation,validation_target_movie)
test_losses.append(to_cpu(loss_test.array))


print('val_loss:{:.04f}'.format(
np.mean(test_losses)))

epoch_idx += 1

这是加载函数

def loader(path_list):

input_movie = [i[0] for i in path_list]
target_movie = [i[1] for i in path_list]

input_movie = np.asarray([[np.asarray(cv2.resize(cv2.imread("../image/" + img),(1024//10,780//10))) for img in img_path] for img_path in input_movie])
target_movie = np.asarray([[np.asarray(cv2.resize(cv2.imread("../image/" + img),(1024//10,780//10))) for img in img_path] for img_path in target_movie])
return tuple([input_movie,target_movie])

这是模型

class conv_3d(Chain):
def __init__(self):
super(conv_3d, self).__init__()
with self.init_scope():
self.conv1 = L.Convolution3D(None,out_channels=3, ksize=3, stride=1, pad=1)
def __call__(self,x):
return F.relu(self.conv1)

我希望火车能正常工作,但出现上述错误。

最佳答案

F.relu(self.conv1) 应固定为 F.relu(self.conv1(x))。您可能还需要将输入发送到 GPU。

关于python - 预期类型错误 : numpy. ndarray 或 cuda.ndarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57471706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com