TensorFlow Basic

Constant

import numpy as np
import tensorflow as tf

sess = tf.Session()

str = tf.constant("this is a string")
str_out = sess.run(str)

print str
# <tf.Tensor 'Const_3:0' shape=() dtype=string>
print str_out
# this is a string
print type(str)
# tensorflow.python.framework.ops.Tensor
print type(str_out)
# str

Operators

a = tf.constant(1.5)
b = tf.constant(2.5)

add = tf.add(a,b)
sub = tf.sub(a,b)
mul = tf.mul(a,b)
div = tf.div(a,b)

out = sess.run([add, sub, mul, div])

out
# [4.0, -1.0, 3.75, 0.60000002]

a
# <tf.Tensor 'Const_1:0' shape=() dtype=float32>

out = sess.run([add, sub, mul, div, a, b])

out
# [4.0, -1.0, 3.75, 0.60000002, 1.5, 2.5]

Variables & PlaceHolder

x = np.random.rand(1,20)

input = tf.placeholder(tf.float32, [None, 20])
weight = tf.Variable(tf.random_normal([20,10], stddev=0.5))
bias = tf.Variable(tf.zeros(1,10))

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

res = tf.matmul(input, weight) + bias
ope = sess.run(res, feed_dict={input:x})

ope
#array([[-0.40633124,  0.68763137,  0.10974612,  0.76354152, -0.62609732,
#        -1.30937958,  0.75056463, -1.05237997,  1.68404746, -1.90896893]], dtype=float32)
#inital result:
weight.eval(sess)
array([[  1.51647525e-02,  -4.06087071e-01,   5.98981380e-01,
          3.19169811e-03,  -7.35296428e-01,  -1.93816647e-01,
         -3.08905631e-01,   8.71859491e-02,   7.42202997e-01,
         -5.65984607e-01],
       [ -3.30144703e-01,  -1.59677058e-01,  -5.77629060e-02,
          5.45485079e-01,   1.68608651e-01,   1.76908046e-01,
          1.84410766e-01,   3.63955110e-01,  -1.23337828e-01,
          2.34381810e-01],
       [ -2.83635035e-02,   3.48388463e-01,  -1.72496751e-01,
          5.47236323e-01,   4.35048074e-01,  -3.48430395e-01,
          4.59737420e-01,  -7.00711370e-01,   5.15151024e-02,
         -4.99038279e-01],
       [ -2.54160613e-01,  -6.12228690e-03,  -1.22292924e+00,
         -7.51280129e-01,   3.29493582e-01,   1.03831682e-02,
          4.47920531e-01,   6.79351926e-01,  -1.50668457e-01,
          1.83695987e-01],
       [  3.39940935e-01,  -6.56426609e-01,   3.47022265e-02,
         -4.44911160e-02,  -4.60504025e-01,  -1.68241382e-01,
          4.92879122e-01,  -4.49492455e-01,   9.26157117e-01,
         -2.94629246e-01],
       [ -9.06509981e-02,  -7.16808796e-01,  -2.19049845e-02,
         -5.28830960e-02,  -1.58434719e-01,  -3.74090433e-01,
          7.81695902e-01,   1.97567090e-01,   4.51313734e-01,
         -6.94154620e-01],
       [  3.09450656e-01,   6.48759365e-01,   1.18139005e+00,
          8.34140554e-02,   4.59507346e-01,  -1.29748821e-01,
         -1.36472836e-01,  -4.55930084e-02,   5.85081220e-01,
         -1.94015764e-02],
       [ -1.04562357e-01,  -3.87483180e-01,  -6.23215735e-01,
         -2.27256745e-01,   1.81832448e-01,  -1.11958909e+00,
         -5.03850468e-02,  -3.06994975e-01,  -4.86681998e-01,
          7.47927129e-01],
       [  8.80965032e-04,   9.89205956e-01,  -1.94297209e-01,
          1.60042405e-01,  -2.87876308e-01,  -8.59210566e-02,
          4.15776342e-01,  -4.70732123e-01,  -8.20633292e-01,
         -8.77852798e-01],
       [ -5.25490880e-01,   9.89412010e-01,  -5.17587543e-01,
          4.95104492e-02,   6.22790337e-01,   4.13729936e-01,
         -4.95980948e-01,  -1.00999981e-01,   3.94825757e-01,
         -2.12808162e-01],
       [ -3.78946185e-01,  -6.66403353e-01,   2.64581650e-01,
          2.38461211e-01,   1.08750106e-03,   3.49856704e-01,
          5.68941355e-01,   1.81829780e-01,  -2.31271088e-01,
         -3.82623643e-01],
       [  5.36305122e-02,   1.23590755e+00,   5.71798444e-01,
          1.10375606e-01,  -3.65412235e-01,   1.33042365e-01,
         -3.21085565e-02,   4.49239343e-01,  -4.53887641e-01,
          2.81042773e-02],
       [  2.00422868e-01,   1.50872886e-01,   1.60236895e-01,
         -8.71270001e-01,  -1.19082320e+00,  -4.89674777e-01,
          3.75180125e-01,  -3.11871409e-01,   3.26364636e-01,
         -8.45709980e-01],
       [ -3.65855277e-01,  -1.58030719e-01,   2.60252237e-01,
         -1.06237307e-01,  -5.21013021e-01,   2.60545164e-01,
          5.17874539e-01,  -2.84448594e-01,  -3.97738278e-01,
          1.39550436e+00],
       [  1.94309801e-01,  -7.44600743e-02,   6.21412158e-01,
         -2.00101644e-01,  -8.86148736e-02,   9.03656662e-01,
         -3.35351050e-01,  -2.94715196e-01,  -9.09028888e-01,
         -7.00205326e-01],
       [ -1.15573434e-02,  -1.95869729e-01,   1.31865144e-01,
          8.79730821e-01,  -2.23836586e-01,  -5.81506304e-02,
          1.50396809e-01,   7.35526755e-02,   3.70406993e-02,
         -2.03212231e-01],
       [ -3.82666796e-01,   3.69342029e-01,  -4.52991202e-02,
          1.57191772e-02,   9.03125554e-02,   1.78596377e-01,
          6.30984306e-01,   8.51772130e-02,   2.58469522e-01,
         -1.81731477e-01],
       [  4.87475693e-01,   4.21634257e-01,  -4.68556106e-01,
          5.64301372e-01,   4.19512449e-04,   5.09169161e-01,
         -8.36884916e-01,   1.57199398e-01,  -8.72832090e-02,
         -1.41665292e+00],
       [ -3.23474824e-01,   9.90427732e-01,   2.46586859e-01,
          5.62765300e-01,  -1.93077654e-01,   3.56368423e-01,
          5.78876078e-01,  -5.05253494e-01,  -3.85444701e-01,
          4.25137043e-01],
       [ -3.61770950e-02,  -3.30416739e-01,  -6.15031719e-01,
         -5.13051093e-01,   6.10428989e-01,  -4.76336271e-01,
         -1.05030549e+00,   1.00477815e-01,   5.79703748e-01,
         -4.55922127e-01]], dtype=float32)
sess.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值