python代码过程中遇到的问题汇总

tensorflow报错,版本问题。高版本到低版本使用:

import tensorflow.compat.v1 as tf

 

tf.placeholder() is not compatible with eager execution. 

2.0版本出现,加一句代码即可

tf.compat.v1.disable_eager_execution()

 

如何读取tensorflow训练好的模型

saver = tf.train.Saver()
with tf.Session() as sess:

print("从指定的路径中加载模型。。。。")            # 将模型加载到sess 中

ckpt = tf.train.get_checkpoint_state(logs_train_dir)

if ckpt and ckpt.model_checkpoint_path:

    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]

    saver.restore(sess, ckpt.model_checkpoint_path)

我们是这样做的

saver = tf.train.Saver()
sess.run(tf.initialize_all_variables())
checkpoint = tf.train.get_checkpoint_state("saved_networks")  

#model_checkpoint_path保存了最新的tensorflow模型文件的文件名
if checkpoint and checkpoint.model_checkpoint_path:
saver.restore(sess, checkpoint.model_checkpoint_path)
print("Successfully loaded:", checkpoint.model_checkpoint_path)
else:
print("Could not find old network weights")

 

tensorflow因为版本是2无法直接下载mnist数据集的办法

两句搞定:

mint = tf.keras.datasets.mnist
(x_,y_),(x_1,y_1) = mint.load_data()

 

imshow和plt.show的区别

imshow()接收一张图像,只是画出该图,并不会立刻显示出来。

imshow后还可以进行其他draw操作,比如scatter散点等。

所有画完后使用plt.show()才能进行结果的显示。

 

 

 

 

 tf.reduce_mean()

tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。

posted @ 2021-04-09 15:00  灰人  阅读(268)  评论(0)    收藏  举报