tensorflow-基础语法

数学公式API:https://github.com/tensorflow/docs/blob/master/site/en/api_guides/python

constant

a = tf.constant(0, name='B')
b = tf.constant(1)

常量

x = tf.zeros([2, 3], tf.int32)
y = tf.zeros_like(x, optimize=True)

变量

with tf.variable_scope('meh') as scope:
a = tf.get_variable('a', [10])
b = tf.get_variable('b', [100])

writer = tf.summary.FileWriter('./graphs/test', tf.get_default_graph())
writer.close()

placeholdder占位符

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))

类型转换

tf.cast(tf.constant(2.0), tf.int32)

把numpy转换成Tensor

import numpy as np
a = np.zeros((3,3))
print(a)
print('----------------')
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:
print(sess.run(ta))
------ 本文结束 🎉🎉 谢谢观看 ------
0%