【问题标题】:TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'.类型错误:>> 不支持的操作数类型:“builtin_function_or_method”和“_io.TextIOWrapper”。
【发布时间】:2019-04-05 07:38:22
【问题描述】:

这是我的代码:

fout = open(path + "/log.txt", "w")
while (True):
    mini_batch = train_graph_data.sample(config.batch_size)
    loss = model.fit(mini_batch)
    batch_n += 1
    if train_graph_data.is_epoch_end:
        epochs += 1
        batch_n = 0
        loss = 0
        if epochs % config.display == 0:
            embedding = None
            while (True):
                mini_batch = train_graph_data.sample(config.batch_size, do_shuffle=False)
                loss += model.get_loss(mini_batch)
                if embedding is None:
                    embedding = model.get_embedding(mini_batch)
                else:
                    embedding = np.vstack((embedding, model.get_embedding(mini_batch)))
                if train_graph_data.is_epoch_end:
                    break
            if config.check_reconstruction:
                print >> fout, epochs, "reconstruction:", check_reconstruction(embedding, train_graph_data,
                                                                               config.check_reconstruction)
            if config.check_link_prediction:
                print >> fout, epochs, "link_prediction:", check_link_prediction(embedding, train_graph_data,
                                                                                 origin_graph_data,
                                                                                 config.check_link_prediction)
            if config.check_classification:
                data = train_graph_data.sample(train_graph_data.N, do_shuffle=False, with_label=True)
                print >> fout, epochs, "classification", check_multi_label_classification(embedding, data.label)
            fout.flush()
            model.save_model(path + '/epoch' + str(epochs) + ".model")
        if epochs == config.epochs_limit:
            print("exceed epochs limit terminating")
            break

我得到了这个错误: >> 不支持的操作数类型:“builtin_function_or_method”和“_io.TextIOWrapper”。 我该如何解决?

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    使用下面的代码来修复它而不是print >>fout,"text"(仅在 python 2.x 中支持)。

    fileOb=open("myoutput.txt","w")
    print("text you want to print",file=fileOb)
    

    如有疑问请咨询How to redirect the output of print to a TXT file

    【讨论】:

    • 如何更改此代码print >> fout, epochs, "reconstruction:", check_reconstruction(embedding, train_graph_data,config.check_reconstruction)
    • 使用print( "reconstruction:", check_reconstruction(embedding, train_graph_data,config.check_reconstruction),file=fout)
    • 有4个参数,epochs我应该在哪里修改?
    • 我忘了添加它们。您可以添加任意数量的参数,只要它们在 file = fileOb(和任何其他关键字参数)之前。使用使用print( epochs,"reconstruction:", check_reconstruction(embedding, train_graph_data,config.check_reconstruction),file=fout)
    猜你喜欢
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    相关资源
    最近更新 更多