TF Girls——tensorflow基本使用

视频:https://space.bilibili.com/16696495/channel/detail?cid=1588

github:https://github.com/CreatCodeBuild/TensorFlow-and-DeepLearning-Tutorial/blob/master/Season2/1%20Word2Vec/word2vec_tf.py

 

 

# encoding: utf-8

# 为了 Python3 的兼容,如果你用的 Python2.7

from __future__ import print_function, division

import tensorflow as tf



print('Loaded TF version', tf.__version__, '\n\n')



# Tensor 在数学中是“张量”

# 标量,矢量/向量,张量



# 简单地理解

# 标量表示值

# 矢量表示位置(空间中的一个点)

# 张量表示整个空间



# 一维数组是矢量

# 多维数组是张量, 矩阵也是张量





# 4个重要的类型

# @Variable		计算图谱中的变量

# @Tensor		一个多维矩阵,带有很多方法

# @Graph		一个计算图谱

# @Session		用来运行一个计算图谱





# 三个重要的函数



# Variable 变量

# tf.Variable.__init__(

#	initial_value=None, @Tensor

#	trainable=True,

#	collections=None,

#	validate_shape=True,

#	caching_device=None,

#	name=None,

#	variable_def=None,

#	dtype=None)

# 注意:Variable是一个Class,Tensor也是一个Class



# Constant 常数

# tf.constant(value, dtype=None, shape=None, name='Const')

# return: a constant @Tensor



# Placeholder 暂时变量?

# tf.placeholder(dtype, shape=None, name=None)

# return: 一个还尚未存在的 @Tensor







# 让我们用计算图谱来实现一些简单的函数

# + - * / 四则运算

def basic_operation():

	v1 = tf.Variable(10)

	v2 = tf.Variable(5)

	addv = v1 + v2

	print(addv)

	print(type(addv))

	print(type(v1))



	c1 = tf.constant(10)

	c2 = tf.constant(5)

	addc = c1 + c2

	print(addc)

	print(type(addc))

	print(type(c1))



	# 用来运行计算图谱的对象/实例?

	# session is a runtime

	sess = tf.Session()



	# Variable -> 初始化 -> 有值的Tensor

	tf.initialize_all_variables().run(session=sess)



	print('变量是需要初始化的')

	print('加法(v1, v2) = ', addv.eval(session=sess))

	print('加法(v1, v2) = ', sess.run(addv))

	print('加法(c1, c2) = ', addc.eval(session=sess))

	print('\n\n')

	#这种定义操作,再执行操作的模式被称之为“符号式编程” Symbolic Programming



	# tf.Graph.__init__()

	# Creates a new, empty Graph.

	graph = tf.Graph()

	with graph.as_default():

		value1 = tf.constant([1,2])

		value2 = tf.Variable([3,4])

		mul = value1 / value2



	with tf.Session(graph=graph) as mySess:

		tf.initialize_all_variables().run()

		print('一一对应的除法(value1, value2) = ', mySess.run(mul))

		print('一一对应的除法(value1, value2) = ', mul.eval())



	# tensor.eval(session=sess)

	# sess.run(tensor)





# 省内存?placeholder才是王道

# def use_placeholder():

	graph = tf.Graph()

	with graph.as_default():

		value1 = tf.placeholder(dtype=tf.float64)

		value2 = tf.Variable([3, 4], dtype=tf.float64)

		mul = value1 * value2



	with tf.Session(graph=graph) as mySess:

		tf.initialize_all_variables().run()

		# 我们想象一下这个数据是从远程加载进来的

		# 文件,网络

		# 假装是 10 GB

		value = load_from_remote()

		for partialValue in load_partial(value, 2):

			# runResult = mySess.run(mul, feed_dict={value1: partialValue})

			evalResult = mul.eval(feed_dict={value1: partialValue})

			print('乘法(value1, value2) = ', runResult)

		# cross validation



def load_from_remote():

	return [-x for x in range(1000)]





# 自定义的 Iterator

# yield, generator function

def load_partial(value, step):

	index = 0

	while index < len(value):

		yield value[index:index+step]

		index += step

	return



if __name__ == '__main__':

	basic_operation()

	# use_placeholder()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值