Dog-go 2020-08-06 20:47 采纳率: 0%
浏览 617
已采纳

在调用'slice_input_producer'时bao'c

运行以下代码,报错:
module 'tensorflow._api.v2.train' has no attribute 'slice_input_producer'

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()
import os  
import numpy as np  
import tensorflow as tf  
#import input_data     
#import model  


N_CLASSES = 2  # 2个输出神经元,[1,0] 或者 [0,1]猫和狗的概率
IMG_W = 208  # 重新定义图片的大小,图片如果过大则训练比较慢  
IMG_H = 208  
BATCH_SIZE = 32  #每批数据的大小
CAPACITY = 256  
MAX_STEP = 15000 # 训练的步数,应当 >= 10000
learning_rate = 0.0001 # 学习率,建议刚开始的 learning_rate <= 0.0001


def run_training():  

    # 数据集
    train_dir = 'D:/Python/data/DogVsCat/kaggle/train'   #My dir--20170727-csq  
    #logs_train_dir 存放训练模型的过程的数据,在tensorboard 中查看 
    logs_train_dir = 'D:/Python/data/DogVsCat/kaggle/'

    # 获取图片和标签集
    train, train_label = get_files(train_dir)  
    # 生成批次
    train_batch, train_label_batch = get_batch(train,  
                                                    train_label,  
                                                    IMG_W,  
                                                    IMG_H,  
                                                    BATCH_SIZE,   
                                                    CAPACITY)
    # 进入模型
    train_logits = inference(train_batch, BATCH_SIZE, N_CLASSES) 
    # 获取 loss 
    train_loss = losses(train_logits, train_label_batch)
    # 训练 
    train_op = trainning(train_loss, learning_rate)
    # 获取准确率 
    train__acc = evaluation(train_logits, train_label_batch)  
    # 合并 summary
    summary_op = tf.summary.merge_all()  
    sess = tf.Session()
    # 保存summary
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)  
    saver = tf.train.Saver()  

    sess.run(tf.global_variables_initializer())  
    coord = tf.train.Coordinator()  
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)  

    try:  
        for step in np.arange(MAX_STEP):  
            if coord.should_stop():  
                    break  
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])  

            if step % 50 == 0:  
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0))  
                summary_str = sess.run(summary_op)  
                train_writer.add_summary(summary_str, step)  

            if step % 2000 == 0 or (step + 1) == MAX_STEP:  
                # 每隔2000步保存一下模型,模型保存在 checkpoint_path 中
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')  
                saver.save(sess, checkpoint_path, global_step=step)  

    except tf.errors.OutOfRangeError:  
        print('Done training -- epoch limit reached')  
    finally:  
        coord.request_stop()
    coord.join(threads)  
    sess.close()  

# train
run_training()

是不是版本问题?我的版本是2.0以上的。

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-08-07 08:32
    关注

    是的,你的代码是 tf 2.0,api改动很大,以前的api可以在 tf.compat.v1 里面找到。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的