首页 > 解决方案 > 如何解决此错误?AttributeError:“builtin_function_or_method”对象没有属性“数据”

问题描述

我正在尝试使用本文中的代码来实现神经风格迁移。当我运行 main.py 时,我的问题标题中出现错误,指的是第 12 行。我很确定它与此有关,torch.randn(input.data.size())但我不知道该怎么做。我仍然是机器学习的初学者,我还在学习有关 Python 的东西。任何帮助,将不胜感激。

from StyleCNN import *
from utils import *

# CUDA Configurations
dtype = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor

# Content and style
style = image_loader("style.jpg").type(dtype)
content = image_loader("content.jpg").type(dtype)

pastiche = image_loader("content.jpg").type(dtype)
pastiche.data = torch.randn(input.data.size()).type(dtype)

num_epochs = 31


def main():
    style_cnn = StyleCNN(style, content, pastiche)

    for i in range(num_epochs):
        pastiche = style_cnn.train()

        if i % 10 == 0:
            print("Iteration: %d" % (i))

            path = "outputs/%d.png" % (i)
            pastiche.data.clamp_(0, 1)
            save_image(pastiche, path)


main()

标签: pythonmachine-learningpytorch

解决方案


推荐阅读