欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mac下tensorflow的helloworld(conda)

程序员文章站 2022-07-08 16:42:46
...
基本的
virtualenv tensorenv
source bin/activate

pip install --upgrade tensorflow

python
>>> import tensorflow as tf
>>> hello  = tf.constant('hello TensorFlow!')
>>> sess =  tf.Session()
>>> print sess.run(hello)
hello TensorFlow!




tensorenv/lib/python2.7/site-packages/tensorflow/examples/tutorials/mnist


#########################

安装anaconda

https://www.anaconda.com/download/#macos


安装之后参考
https://blog.csdn.net/lq_547762983/article/details/81003528

conda list

conda create -n tensorenv tensorflow
conda env list


创建名字为tensorenv 的环境并安装tensorflow
安装在/anaconda2/envs 下
/anaconda2/envs/tensorenv



source activate tensorenv





如果需要装其他包继续
conda install -n tensorenv numpy



下载数据:
参考http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html

https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/input_data.py
添加
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

用自己的网,网络限制就想其他办法




反向传播
梯度下降



dropout 解决泛化性不好

无监督学习 https://www.zhihu.com/question/23194489


##################
使用的tf1.0的版本,很多api变了

第一个helloworld

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

a = tf.constant(5,name="input_a")
b = tf.constant(3,name="input_b")
c = tf.multiply(a,b,name="mul_c")
d = tf.add(a,b,name="add_d")
e = tf.add(c,d,name="add_e")
sess = tf.Session()
sess.run(e)

#writer = tf.train.SummaryWriter('./my_grap',sess.graph)
writer = tf.summary.FileWriter('./my_graph',sess.graph)
writer.close()
sess.close()


tensorboard --logdir="./my_graph"


得到mac下tensorflow的helloworld(conda)
            
    
    博客分类: tensorflowconda tensorflowconda 

张量的helloworld
test2.py
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

a = tf.constant([5,3],name="input_a")
b = tf.reduce_prod(a,name="prod_b")
c = tf.reduce_sum(a,name="sum_c")
d = tf.add(c,b,name="add_d")
sess =  tf.Session()
sess.run(d)

writer = tf.summary.FileWriter('./my_test2',sess.graph)
writer.close()
sess.close()


numpy的helloworld

注意:numpy使用字符串时,不要显示的指定dtype属性



基本概念
tensor -->numpy建立     a = tf.constant(3,name="input_b")
opration ---> b = tf.add(a,3) ;
graph ---> g=tf.Graph()  g.as_default()
session  --->sess = ts.Session(graph = tf.get_default_graph())
fetches ---->sesion.run(a) ;sesion.run([a,b])
feed_dict ----> sesion.run(b,feed_dict = {a:15})   ---改变参数的值,测试和中间环节改变值调试用,或者是给占位符赋值
placeholder(dtype,shape,name) ---> tf.placeholder(tf.int32,shape=[2])     ---> dtype参数是必须指定的

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

a = tf.add(2,5)
b = tf.multiply(a,3)

sess = tf.Session()
replace_dict = {a:15}

c = sess.run(b,feed_dict=replace_dict)
print(c)



基本机器学习
basic.py
import tensorflow as tf
import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def inference(X):
	print("inference")

def loss(X,Y):
	print("loss")
	loss_v = tf.constant("myloss",name="loss_value")
	return loss_v

def inputs():
	print("input")
	return 1,2

def train(total_loss):
	print("train")
	hello = tf.constant("ha",name="train_value")
	return hello

def evaluate(sess,X,Y):
	print("evaluate")


with tf.Session() as sess:
#	tf.initialize_all_variables().run()
	tf.global_variables_initializer().run()
	X,Y = inputs()

	total_loss = loss(X,Y)
	train_op = train(total_loss)
	coord = tf.train.Coordinator()
	threads = tf.train.start_queue_runners(sess=sess,coord=coord)
	training_steps = 100
	for step in range(training_steps):
		sess.run([train_op])
		if step % 10 ==0:
			print("loss:",sess.run([total_loss]))
	evaluate(sess,X,Y)

	coord.request_stop()
	coord.join(threads)
	sess.close()



检查点,针对变量的
注意:变量不能放在定义的方法里
saver要放在初始化变量之后
check.py
import tensorflow as tf
import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def inference(X):
	print("inference")

def loss(X,Y):
	print("loss")
	return X

def inputs(X):
	print("input")
	Y = tf.constant("myY",name="Y_value")
	return X,Y

def train(total_loss):
	print("train")
	hello = tf.constant("ha",name="train_value")
	return hello

def evaluate(sess,X,Y):
	print("evaluate")
with tf.Session() as sess:
	#tf.global_variables_initializer().run()
	thisX = tf.Variable("thisX",name="thisX_value")
	sess.run(tf.global_variables_initializer())
	saver = tf.train.Saver()
	X,Y = inputs(thisX)

	total_loss = loss(X,Y)
	train_op = train(total_loss)
	coord = tf.train.Coordinator()
	threads = tf.train.start_queue_runners(sess=sess,coord=coord)
	training_steps = 100
	for step in range(training_steps):
		sess.run([train_op])
		if step % 10 ==0:
			print("loss:",sess.run([total_loss]))
			saver.save(sess,'./work/my-model',global_step=step)
	evaluate(sess,X,Y)

	coord.request_stop()
	coord.join(threads)
	saver.save(sess,'./work/my-model',global_step=training_steps)
	sess.close()


恢复检查点
restore.py

import tensorflow as tf
import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def inference(X):
	print("inference")

def loss(X,Y):
	print("loss")
	return X

def inputs(X):
	print("input")
	Y = tf.constant("myY",name="Y_value")
	return X,Y

def train(total_loss):
	print("train")
	hello = tf.constant("ha",name="train_value")
	return hello

def evaluate(sess,X,Y):
	print("evaluate")
with tf.Session() as sess:
	#tf.global_variables_initializer().run()
	thisX = tf.Variable("thisX",name="thisX_value")
	sess.run(tf.global_variables_initializer())
	saver = tf.train.Saver()
	X,Y = inputs(thisX)

	total_loss = loss(X,Y)
	train_op = train(total_loss)
	coord = tf.train.Coordinator()
	threads = tf.train.start_queue_runners(sess=sess,coord=coord)
	training_steps = 1000

	initial_step = 0
	ckpt = tf.train.get_checkpoint_state(os.path.dirname("./work/my-model"))
	print("debug---->0 "+str(ckpt))
	if ckpt and ckpt.model_checkpoint_path:
		print("debug---->1")
		saver.restore(sess,ckpt.model_checkpoint_path)
		initial_step =  int(ckpt.model_checkpoint_path.rsplit('-',1)[1])
		print("debug---->2.1 " + str(initial_step))
		print("debug---->2.2 " + str(initial_step))
		for step in range( initial_step,training_steps):
			print("debug---->3")
			sess.run([train_op])
			if step % 10 ==0:
				print("loss:",sess.run([total_loss]))
				saver.save(sess,'./work/my-model',global_step=step)
	evaluate(sess,X,Y)

	coord.request_stop()
	coord.join(threads)
	saver.save(sess,'./work/my-model',global_step=training_steps)
	sess.close()

关于saver

https://www.tensorflow.org/programmers_guide/saved_model?hl=zh-cn




  • mac下tensorflow的helloworld(conda)
            
    
    博客分类: tensorflowconda tensorflowconda 
  • 大小: 50.6 KB
相关标签: tensorflow conda