Create Graph

C3:Uderstand TensorFlow Basics

How to do it?

a = tf.constant(5)
b = tf.constant(2)
c = tf.constant(3)
d = tf.multiply(a, b)
e = tf.add(c, b)
f = tf.subtract(d, e)
with tf.Session() as sess:
    ans = sess.run(f)
print(ans)

exercise

C3:Uderstand TensorFlow Basics

C3:Uderstand TensorFlow Basics

How to do it?

A

a = tf.constant(6)
b = tf.constant(6)
c = tf.multiply(a, b)
d = tf.add(a, b)
e = tf.subtract(d, c)
f = tf.add(c, d)
g = tf.divide(f, e)
with tf.Session() as sess:
    ans = sess.run(g)
print(ans)

B

a = tf.constant(6)
b = tf.constant(6)
c = tf.multiply(a, b)
sess = tf.Session()
d = tf.sin(float(sess.run(c)))
e = tf.divide(float(sess.run(b)), d)
e = sess.run(e)
sess.close()
print(e)

Constructing and Managing Our Graph

print(tf.get_default_graph())
g = tf.Graph()
print(g)
a = tf.constant(5)
print(a.graph is g)
print(a.graph is tf.get_default_graph())
print("------------------------")
g1 = tf.get_default_graph()
g2 = tf.Graph()
print(g1 is tf.get_default_graph())
with g2.as_default():
    print(g1 is tf.get_default_graph())
print(g1 is tf.get_default_graph())

Display:

C3:Uderstand TensorFlow Basics

Fetches

a = tf.constant(6)
b = tf.constant(6)
c = tf.multiply(a, b)
d = tf.add(a, b)
e = tf.subtract(d, c)
f = tf.add(c, d)
with tf.Session() as sess:
    fetches = [a,b,c,d,e,f]
    outs = sess.run(fetches)
print("outs = {}".format(outs))
print(type(outs[0]))

Display:

C3:Uderstand TensorFlow Basics

Data Types

c = tf.constant(4.0, dtype=tf.float64)
print(c)
print(c.dtype)

Display:

C3:Uderstand TensorFlow Basics

Casting

x = tf.constant([1,2,3],name='x',dtype=tf.float32)
print(x.dtype)
x = tf.cast(x,tf.int64)
print(x.dtype)

Display:

C3:Uderstand TensorFlow Basics

C3:Uderstand TensorFlow Basics

Tensor Arrays and Shapes

c = tf.constant([[1,2,3],[4,5,6]])
print("python list input:{}".format(c.get_shape()))
c = tf.constant(np.array([[[1,2,3],[4,5,6]],[[1,1,1],[2,2,2]]]))
print("3d numpy array input:{}".format(c.get_shape()))

Display:

C3:Uderstand TensorFlow Basics

InteractiveSession

sess = tf.InteractiveSession()
c = tf.linspace(0.0,4.0,5)
print("the content of 'c':\n {}\n".format(c.eval()))
sess.close()

Display:

C3:Uderstand TensorFlow Basics

C3:Uderstand TensorFlow Basics

Matrix multiplication

A = tf.constant([ [1,2,3],[4,5,6] ])
print(A.get_shape())
x = tf.constant([1,0,1])
print(x.get_shape())

Display:

C3:Uderstand TensorFlow Basics

x = tf.expand_dims(x,1)

print(x.get_shape())

b = tf.matmul(A,x)

sess = tf.InteractiveSession()

print("matmul result:\n{}".format(b.eval()))

sess.close()

Display:

C3:Uderstand TensorFlow Basics

Names

with tf.Graph().as_default():
    c1 = tf.constant(4,dtype=tf.float64,name="c")
    c2 = tf.constant(4,dtype=tf.int32,name="c")
print(c1.name)
print(c2.name)

Display:

C3:Uderstand TensorFlow Basics

Name Scopes

with tf.Graph().as_default():
    c1 = tf.constant(4,dtype=tf.float64,name="c")
    with tf.name_scope("prefix_name"):
        c2 = tf.constant(4,dtype=tf.int32,name="c")
        c3 = tf.constant(4,dtype=tf.float64,name="c")
print(c1.name)
print(c2.name)
print(c3.name)

Display:

C3:Uderstand TensorFlow Basics

Variables

init_val = tf.random_normal((1,5),0,1)
var = tf.Variable(init_val,name="var")
print("pre run:\n{}".format(var))
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    post_var = sess.run(var)
print("\npost run:\n{}".format(post_var))

Display:

C3:Uderstand TensorFlow Basics

Placeholders

x_data = np.random.randn(5,10)
w_data = np.random.randn(10,1)
with tf.Graph().as_default():
    x = tf.placeholder(tf.float32,shape=(5,10))
    w = tf.placeholder(tf.float32,shape=(10,1))
    b = tf.fill((5,1),-1.)
    xw = tf.matmul(x,w)
    xwb = xw + b
    s = tf.reduce_max(xwb)
    with tf.Session() as sess:
        outs = sess.run(s,feed_dict={x:x_data,w:w_data})
print("outs = {}".format(outs))

Display:

C3:Uderstand TensorFlow Basics

Optimization

Model:

C3:Uderstand TensorFlow Basics

How to do it?

x = tf.placeholder(tf.float32,shape = [None,3])
y_true = tf.placeholder(tf.float32,shape=None)
w = tf.Variable([[0,0,0]],dtype=tf.float32,name="weights")
b = tf.Variable(0,dtype=tf.float32,name="bias")
y_pred = tf.matmul(w,tf.transpose(x)) + b

MSE and cross entropy

Model:

C3:Uderstand TensorFlow Basics

How to do it?

Loss = tf.reduce_mean(tf.square(y_true-y_pred))

Model:

C3:Uderstand TensorFlow Basics

How to do it?

Loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y_true,logits = y_pred)
Loss = tf.reduce_mean(loss)

Gradient descent in TensorFlow

Optimizer = tf.train.GradientDescentOptimizer(learning_rate)
Train = optimizer.minimize(loss)

Liner regression

x_data = np.random.randn(2000,3)
w_real = [0.3,0.5,0.1]
b_real = -0.2
noise = np.random.randn(1,2000)*0.1
y_data = np.matmul(w_real,x_data.T)+b_real+noise

Esimate

NUM_STEPS = 10
g = tf.Graph()
wb_ = []
with g.as_default():
    x = tf.placeholder(tf.float32,shape=[None,3])
    y_true = tf.placeholder(tf.float32,shape=None)
    with tf.name_scope("inference") as scope:
        w = tf.Variable([[0,0,0]],dtype=tf.float32,name='weights')
        b = tf.Variable(0,dtype=tf.float32,name='bias')
        y_pred = tf.matmul(w,tf.transpose(x))+b
    with tf.name_scope("loss") as scope:
        loss = tf.reduce_mean(tf.square(y_true-y_pred))
    with tf.name_scope("train") as scope:
        learning_rate = 0.5
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
        train = optimizer.minimize(loss)
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for step in range(NUM_STEPS):
            sess.run(train,{x:x_data,y_true:y_data})
            if(step%5 == 0):
                print(step,sess.run([w,b]))
                wb_.append(sess.run([w,b]))
        print(10,sess.run([w,b]))

Display:

C3:Uderstand TensorFlow Basics

Logistic regression

Model

C3:Uderstand TensorFlow Basics

How to do it?

N = 20000
def sigmoid(x):
    return 1/(1+np.exp(-x))
x_data = np.random.randn(N,3)
w_real = [0.3,0.5,0.1]
b_real = -0.2
wxb = np.matmul(w_real,x_data.T)+b_real
y_data_pre_noise = sigmoid(wxb)
y_data = np.random.binomial(1,y_data_pre_noise)

Esimate

NUM_STEPS = 50
g = tf.Graph()
wb_ = []
with g.as_default():
    x = tf.placeholder(tf.float32,shape=[None,3])
    y_true = tf.placeholder(tf.float32,shape=None)
    with tf.name_scope('inference') as scope:
        w = tf.Variable([[0,0,0]],dtype=tf.float32,name='weights')
        b = tf.Variable(0,dtype=tf.float32,name='bias')
        y_pred = tf.matmul(w,tf.transpose(x)) + b
    with tf.name_scope('loss') as scope:
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y_true,logits=y_pred) 
        loss = tf.reduce_mean(loss)
    with tf.name_scope('train') as scope:
        learning_rate = 0.5
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
        train = optimizer.minimize(loss)
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for step in range(NUM_STEPS):
            sess.run(train,{x:x_data,y_true:y_data})
            if(step%5 == 0):
                print(step,sess.run([w,b]))
                wb_.append(sess.run([w,b]))
        print(50,sess.run([w,b]))

Display:

C3:Uderstand TensorFlow Basics

 

相关文章:

  • 2022-01-10
  • 2021-09-30
  • 2021-05-19
  • 2021-07-10
  • 2021-06-01
  • 2021-08-10
  • 2021-06-13
猜你喜欢
  • 2021-08-07
  • 2022-12-23
  • 2022-12-23
  • 2021-06-05
  • 2021-07-25
  • 2022-12-23
  • 2021-07-06
相关资源
相似解决方案