module 'tensorflow' has no attribute 'reset_default_graph'
A Neural Probabilistic Language Model 论文阅读及实战
代码复现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-02-26 21:25:01 # @Author : cdl (1217096231@qq.com) # @Link : https://github.com/cdlwhm1217096231/python3_spider # @Version : $Id$ import numpy as np #import tensorflow as tf import tensorflow.compat.v1 as tf tf.disable_v2_behavior() tf.reset_default_graph() sentences = [ "i like coffee" , "i love curry" , "i hate apple" ] word_list = " " .join(sentences).split() word_list = list ( set (word_list)) print (word_list) word_dict = {w: i for i, w in enumerate (word_list)} number_dict = {i: w for i, w in enumerate (word_list)} n_class = len (word_dict) # Model parameters n_step = 2 n_hidden = 5 def make_batch(sentences): input_batch = [] target_batch = [] for sentence in sentences: words = sentence.split() input = [word_dict[word] for word in words[: - 1 ]] target = word_dict[words[ - 1 ]] input_batch.append(np.eye(n_class)[ input ]) # np.eye()是单位对角阵 target_batch.append(np.eye(n_class)[target]) return input_batch, target_batch # Model # [batch_size, number of steps, number of Vocabulary] X = tf.placeholder(tf.float32, [ None , n_step, n_class]) Y = tf.placeholder(tf.float32, [ None , n_class]) # [batch_size, n_step * n_class] input = tf.reshape(X, shape = [ - 1 , n_step * n_class]) H = tf.Variable(tf.random_normal([n_step * n_class, n_hidden])) d = tf.Variable(tf.random_normal([n_hidden])) U = tf.Variable(tf.random_normal([n_hidden, n_class])) b = tf.Variable(tf.random_normal([n_class])) tanh = tf.nn.tanh(d + tf.matmul( input , H)) # [batch_size, n_hidden] output = tf.matmul(tanh, U) + b # [batch_size, n_class] cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits = output, labels = Y)) optimizer = tf.train.AdamOptimizer( 0.001 ).minimize(cost) prediction = tf.argmax(output, 1 ) # Training init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) input_batch, target_batch = make_batch(sentences) for epoch in range ( 5000 ): _, loss = sess.run([optimizer, cost], feed_dict = { X: input_batch, Y: target_batch}) if (epoch + 1 ) % 1000 = = 0 : print ( "Epoch:{}" . format (epoch + 1 ), "Cost:{:.4f}" . format (loss)) # Predict predict = sess.run([prediction], feed_dict = {X: input_batch}) # Test input = [sentence.split()[: 2 ] for sentence in sentences] print ([sentence.split()[: 2 ] for sentence in sentences], '---->' , [number_dict[n] for n in predict[ 0 ]]) |
报错信息如下:
module 'tensorflow' has no attribute 'reset_default_graph'
解决方案如下:
1,原本的代码
import tensorflow as tf #这行代码改成下面的两行代码
2,替换成如下代码:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
运行成功。
---------------------------------------------------------------------------------------------------------------------
如果你觉得文章有用,欢迎打赏。
自然语言处理爱好者,欢迎交流。QQ: 7214218
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)