美文网首页
tensorflow 练习

tensorflow 练习

作者: help_youself | 来源:发表于2021-01-28 10:30 被阅读0次

add.py

import tensorflow.compat.v1 as tf
import  numpy  as  np
import  os
def add_batch_dimension(data,batch=1):
    shape=data.shape
    dim=len(shape)
    if dim==1:
        width=int(len(data)/batch)
        return np.asarray(data[:]).reshape((batch,width))
    elif dim==2:
        height=int(shape[0]/batch)
        width=shape[1]
        return np.asarray(data[:]).reshape((batch,height,width))
    else:
        raise Exception("dimision error")
class Add(object):
    def __init__(self,height,width):
        self.x=tf.placeholder(tf.float32, [None,height,width])
        self.y=tf.placeholder(tf.float32, [None,height,width])
        print(self.x.name)
        self.result=tf.log(tf.reduce_sum(tf.add(self.x,self.y),reduction_indices=0,keepdims=True))
    def Run(self,session,data1,data2):
        print(session.run(self.result, feed_dict={self.x:data1,self.y:data2}))
class Multiply(object):
    def __init__(self,height,width):
        self.x=tf.placeholder(tf.float32, [None,height,width])
        self.y=tf.placeholder(tf.float32, [None,height,width])
        self.result=tf.multiply(self.x,self.y)
    def Run(self,session,data1,data2):
        print(session.run(self.result, feed_dict={self.x:data1,self.y:data2}))
if __name__ == '__main__':
    os.environ["CUDA_VISIBLE_DEVICES"]="0"
    tf.compat.v1.disable_eager_execution()
    a1=np.array([[1,1,3]],dtype=np.float32)
    a2=np.array([[2,2,3]],dtype=np.float32)
    a=np.vstack((a1,a2))
    a_batch=add_batch_dimension(a,2)
    a1=np.array([[4,4,6]],dtype=np.float32)
    a2=np.array([[5,5,6]],dtype=np.float32)
    b=np.vstack((a1,a2))
    b_batch=add_batch_dimension(b,2)
    c=a_batch+b_batch
    print(c)
    print("\n")
    session=tf.Session()
    op=Add(a1.shape[0],a1.shape[1])
    op.Run(session,a_batch,b_batch)
    print("\n\n")
    op=Multiply(a1.shape[0],a1.shape[1])
    op.Run(session,a_batch,b_batch)
    session.close()

linear_regression.py

#https://www.cnblogs.com/zrmw/p/11572329.html
#https://blog.csdn.net/panguangyuu/article/details/87452533
import tensorflow.compat.v1 as tf
import numpy as np
import os
def add_batch_dimension(data,batch=1):
    shape=data.shape
    dim=len(shape)
    if dim==1:
        width=int(len(data)/batch)
        return np.asarray(data[:]).reshape((batch,width))
    elif dim==2:
        height=int(shape[0]/batch)
        width=shape[1]
        return np.asarray(data[:]).reshape((batch,height,width))
    else:
        raise Exception("dimision error")
class Model1():
    def __init__(self,samples):
        self.x=tf.placeholder(dtype=tf.float32, shape=(None, samples))
        self.y=tf.placeholder(dtype=tf.float32, shape=(None, samples))
        self.w=tf.Variable(tf.zeros([1], dtype=tf.float32))
        self.b=tf.Variable(tf.zeros([1], dtype=tf.float32))
        pred=tf.add(tf.multiply(self.x,self.w),self.b)
        self.loss = tf.reduce_mean(tf.square(self.y -pred), name='loss')
        optimizer = tf.train.GradientDescentOptimizer(0.1)
        self.min_op=optimizer.minimize(self.loss, name='train')
        self.count=0
    def train(self,sess,x_vec,y_vec):
        sess.run(self.min_op,feed_dict={self.x:x_vec,self.y:y_vec})
        self.count=self.count+1
        if self.count%20==0:
            w=sess.run(self.w)
            b=sess.run(self.b)
            print(self.count,w,b)
if __name__=='__main__':
    os.environ["CUDA_VISIBLE_DEVICES"]="0"
    tf.disable_v2_behavior()
    x = np.random.rand(100)
    y = 0.1 *x + 0.2
    x_batch=add_batch_dimension(x)
    y_batch=add_batch_dimension(y)
    print (x.shape[0])
    model=Model1(x.shape[0])
    sess=tf.Session()    
    init = tf.global_variables_initializer() 
    sess.run(init)
    for i in range(400):
        model.train(sess,x_batch,y_batch)
    sess.close()

gradient2.py

import numpy as np
import tensorflow.compat.v1 as tf
if __name__ == '__main__':
    tf.compat.v1.disable_eager_execution()

    session = tf.Session()
    x = tf.Variable(initial_value=50., dtype='float32')
    w = tf.Variable(initial_value=10., dtype='float32')
    y = w*x
    
    opt = tf.train.GradientDescentOptimizer(0.1)
    grad_and_var= opt.compute_gradients(y, [w,x])
    session.run(tf.global_variables_initializer())
    print(session.run(grad_and_var))
    grad=tf.gradients(y,[w,x])
    print(session.run(grad))
    session.close()

gradient_descent.py

#https://github.com/paklong/Tensorflow-2.0-gradient-descent/blob/master/gradientdescent.py
#3 different ways to Perform Gradient Descent in Tensorflow 
# https://medium.com/analytics-vidhya/3-different-ways-to-perform-gradient-descent-in-tensorflow-2-0-and-ms-excel-ffc3791a160a
import numpy as np
import tensorflow.compat.v1 as tf

#Function with inputs x1 and x2
def fu(x1, x2): 
    return (x1-2) ** 2 + x2 ** 2

#Reset the values of x1 and x2 for different algorithms
def reset():    
    x1 = tf.Variable(10.0) 
    x2 = tf.Variable(10.0) 
    return x1, x2
if __name__ == '__main__':
    x1, x2 = reset()
    for i in range(30):
        with tf.GradientTape() as tape:
            y = fu(x1, x2)
        grads = tape.gradient(y, [x1, x2])
        print ('y = {:.1f}, x1 = {:.1f}, x2 = {:.1f},  grads0 = {:.1f}, grads1 = {:.1f} '.format(y.numpy(), x1.numpy(), x2.numpy(), grads[0].numpy(), grads[1].numpy()))
        #Update x1, x2
        x1.assign(x1 - 0.1*grads[0].numpy())
        x2.assign(x2 - 0.1*grads[1].numpy())

conv1d.py

# https://www.jianshu.com/p/2915426257ec
import tensorflow.compat.v1 as tf

num_filters = 2
kernel_size = 2
batch_size = 2
seq_length = 4
embedding_dim = 5
#RuntimeError:The Session graph is empty.
tf.compat.v1.disable_eager_execution()

embedding_inputs = tf.constant(-1.0, shape=[batch_size, seq_length, embedding_dim], dtype=tf.float32)
conv = tf.layers.conv1d(embedding_inputs, num_filters, kernel_size, padding='SAME',name='conv')
y=tf.layers.flatten(conv)
init=tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print (sess.run(conv).shape)
    print (sess.run(y).shape)

conv1d_print.py

#https://blog.csdn.net/qq_32797059/article/details/89002348
import tensorflow.compat.v1 as tf
import tflearn
import  os
import numpy as np
FILTER_SIZE=2
def add_batch_dimension(data,batch=1):
    shape=data.shape
    dim=len(shape)
    if dim==1:
        width=int(len(data)/batch)
        return np.asarray(data[:]).reshape((batch,width))
    elif dim==2:
        height=int(shape[0]/batch)
        width=shape[1]
        return np.asarray(data[:]).reshape((batch,height,width))
    else:
        raise Exception("dimision error")

def create_network(inputs):
    with tf.variable_scope('test-layer'):
        layer1 = tflearn.conv_1d(
                inputs[:,:,:],1,FILTER_SIZE,activation='relu')
        return layer1
if __name__ == '__main__':
    os.environ["CUDA_VISIBLE_DEVICES"]="0"
    tf.compat.v1.disable_eager_execution()
    a=np.array([1,2,3,4,5,6,7,8,9],dtype='float32')
    b=np.vstack((a,a,a,a,a,a,a,a))
    height=4
    width=len(a)
    inputs=tf.placeholder(tf.float32, [None, height,width])
    init=tf.global_variables_initializer()
    sess=tf.Session()
    sess.run(init)
    layer=create_network(inputs)
    in_data=add_batch_dimension(b,2)
    #print(sess.run(tf.report_uninitialized_variables()))
    tflearn.is_training(True, session=sess)
    init = tf.initialize_all_variables() 
    sess.run(init)    
    result = sess.run(layer, feed_dict={inputs:in_data})
    print (result)
#http://www.voidcn.com/article/p-rosrumst-brt.html
    for tv in tf.trainable_variables():
        print (tv.name)
    graph = tf.get_default_graph()
    w=graph.get_tensor_by_name("test-layer/Conv1D/W:0")
    print("weight:")
    print (sess.run(w))
    b=graph.get_tensor_by_name("test-layer/Conv1D/b:0")
    print("bias:")
    print (sess.run(b))
    print ("network param:")
    network_params =tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='test-layer')
    for param in network_params:
        print(sess.run(param))
    sess.close()

mutiple graph using thread

graph.py

#https://blog.csdn.net/dcrmg/article/details/79028032
#https://blog.csdn.net/weixin_38314865/article/details/83009367
import tensorflow.compat.v1 as tf
class Network(object):
    def __init__(self):
        self.g=tf.Graph()
        with self.g.as_default():
            self.a=tf.constant([1.0,2.0])
            self.b=tf.constant([1.0,2.0])
    def get_graph(self):
        return self.g
    def Run(self):
        with tf.Session(graph=self.g) as sess:
            tf.compat.v1.disable_eager_execution()
            print (sess.run(self.a+self.b))

test.py

import graph
import time
import threading
class Counter():
    def __init__(self):
        self.c=0
        self.lock=threading.RLock()
    def Add(self):
        self.lock.acquire()
        self.c=self.c+1
        self.lock.release()
    def Value(self):
        return self.c
def agent(lock, list,count):
    network=graph.Network()
    lock.acquire()
    list.append(network.get_graph())
    lock.release()
    network.Run()
    print(threading.current_thread().name+" "+str(id(count)))
    count.Add()
if __name__ == '__main__':
    lock=threading.RLock()
    count=Counter();
    list=[]
    threads=[]
    num=2
    for i in range (num):
        thread=threading.Thread(name='thread'+str(i), target=agent , args=(lock , list,count))
        thread.start()
        threads.append(thread)
    time.sleep( 5 )
    print (count.Value())
    for thread in threads:
        thread.join()
    for item in list:
        print ('graph ={0}'.format(item))

assign.py

import os
import numpy as np
import tensorflow.compat.v1 as tf
def add_batch_dimension(data,batch=1):
    shape=data.shape
    dim=len(shape)
    if dim==1:
        width=int(len(data)/batch)
        return np.asarray(data[:]).reshape((batch,width))
    elif dim==2:
        height=int(shape[0]/batch)
        width=shape[1]
        return np.asarray(data[:]).reshape((batch,height,width))
    else:
        raise Exception("dimision error")
def print_info(session,trainable_var,log_v=False):
    for param in trainable_var:
        print(param.name)
        result=sess.run(param)
        print(result.shape)
        if log_v:
            print(result)
class Model():
    def __init__(self,input_size,output_size,size,name):
        self.input= tf.placeholder(dtype=tf.float32, shape=(None, input_size))
        with tf.variable_scope(name_or_scope=name):
            self.W1 = tf.get_variable('W1', shape=[input_size, size], initializer=tf.glorot_uniform_initializer())
            self.W2 = tf.get_variable('W2', shape=[size, size], initializer=tf.glorot_uniform_initializer())
            self.W3 = tf.get_variable('W3', shape=[size, output_size], initializer=tf.glorot_uniform_initializer())
        
            self.b1 = tf.Variable(tf.zeros([1], dtype=tf.float32))
            self.b2 = tf.Variable(tf.zeros([1], dtype=tf.float32))
        
            self.L1 = tf.nn.relu(tf.matmul(self.input, self.W1) + self.b1)
            self.L2 = tf.nn.relu(tf.matmul(self.L1, self.W2) + self.b2)
            self.Q= tf.matmul(self.L2, self.W3)
        self.trainable_var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, name)
class Model2():
    def __init__(self,input_size,output_size,size,name):
        self.input= tf.placeholder(dtype=tf.float32, shape=(None, input_size))
        with tf.variable_scope(name_or_scope=name):
            self.fc1 = tf.layers.dense(self.input, size, activation=tf.nn.relu)
            self.fc2 = tf.layers.dense(self.fc1, size, activation=tf.nn.relu)
            self.Q= tf.layers.dense(self.fc2, output_size, activation=tf.nn.relu)
        self.trainable_var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, name)            
if __name__=='__main__':
    os.environ["CUDA_VISIBLE_DEVICES"]="0"
    tf.disable_v2_behavior()
    sess=tf.Session()
    model=Model2(4,2,3,"eval")
    target=Model2(4,2,3,"target")
    init = tf.initialize_all_variables() 
    sess.run(init)
    print_info(sess,model.trainable_var)
    print("\n\n")
    print_info(sess,target.trainable_var)
    print("\n\n")
    for i in range(len(model.trainable_var)):
        sess.run(target.trainable_var[i].assign(model.trainable_var[i]))
    print_info(sess,target.trainable_var)    
    sess.close()
    '''
    input_size=4
    name='test'
    input= tf.placeholder(dtype=tf.float32, shape=(None, input_size))
    with tf.variable_scope(name_or_scope=name):
        W1 = tf.get_variable('W1', shape=[input_size,3], initializer=tf.glorot_uniform_initializer())
    out=tf.matmul(input, W1)
    trainable_var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, name)
    sess=tf.Session()
    init = tf.initialize_all_variables()
    sess.run(init)
    data=np.array([1,1,1,1])
    in1=add_batch_dimension(data)
    print_info(sess,trainable_var)
    print("\n\n")
    print(sess.run(out, feed_dict={input:in1}))
    sess.close()
    '''

cartpole_dqn.py

#https://blog.csdn.net/zhangycode/article/details/104681185?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-1&spm=1001.2101.3001.4242
#https://blog.csdn.net/scp_6453/article/details/110727115
import gym
import os
import numpy as np
import random as ran
import tensorflow.compat.v1 as tf
if __name__=='__main__':
    env = gym.make('CartPole-v0')
    REPLAY_MEMORY = []
    MINIBATCH = 50
    INPUT = env.observation_space.shape[0]
    OUTPUT = env.action_space.n
    LEARNING_LATE = 0.001
    DISCOUNT = 0.99
    model_path= "./save/cartpole.ckpt"
    os.environ["CUDA_VISIBLE_DEVICES"]="0"
    tf.disable_v2_behavior()
    x = tf.placeholder(dtype=tf.float32, shape=(None, INPUT), name='x')
    y = tf.placeholder(dtype=tf.float32, shape=(None, OUTPUT), name='y')

    with tf.variable_scope('eval_net'):
    
        W1 = tf.get_variable('W1', shape=[INPUT, 200], initializer=tf.glorot_uniform_initializer())
        W2 = tf.get_variable('W2', shape=[200, 200], initializer=tf.glorot_uniform_initializer())
        W3 = tf.get_variable('W3', shape=[200, OUTPUT], initializer=tf.glorot_uniform_initializer())
    
        b1 = tf.Variable(tf.zeros([1], dtype=tf.float32))
        b2 = tf.Variable(tf.zeros([1], dtype=tf.float32))
    
        L1 = tf.nn.relu(tf.matmul(x, W1) + b1)
        L2 = tf.nn.relu(tf.matmul(L1, W2) + b2)
        Q_pre = tf.matmul(L2, W3)
    with tf.variable_scope('target_net'):
    
        W1_r = tf.get_variable('W1_r', shape=[INPUT, 200])
        W2_r = tf.get_variable('W2_r', shape=[200, 200])
        W3_r = tf.get_variable('W3_r', shape=[200, OUTPUT])
    
        b1_r = tf.Variable(tf.zeros([1], dtype=tf.float32))
        b2_r = tf.Variable(tf.zeros([1], dtype=tf.float32))
    
        L1_r = tf.nn.relu(tf.matmul(x, W1_r) + b1_r)
        L2_r = tf.nn.relu(tf.matmul(L1_r, W2_r) + b2_r)
        Q_pre_r = tf.matmul(L2_r, W3_r)

    recent_rlist = [0]
    episode = 0
    
    #损失函数和优化函数
    cost = tf.reduce_sum(tf.square(y - Q_pre))
    optimizer = tf.train.AdamOptimizer(LEARNING_LATE, epsilon=0.01)
    train = optimizer.minimize(cost)
    
    #更新目标Q网络的参数
    t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='target_net')
    e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='eval_net')
    replace_target_para = [tf.assign(t, e) for t, e in zip(t_params, e_params)]
    #学习过程
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        
        sess.run(replace_target_para)
    
        while np.mean(recent_rlist) < 195:
            episode += 1
            #每个episode都要将环境恢复初始状态
            s = env.reset()
            #超过200 episode时,用最新的reward更新列表
            if len(recent_rlist) > 200:
                del recent_rlist[0]
            # e-greedy
            e = 1. / ((episode / 25) + 1)
    
            rall = 0  # reward_all
            d = False  #杆子是否倒下,即回合结束
            count = 0 #一个episode里训练的步数
    
            while not d and count < env.spec.max_episode_steps:
    
                # env.render()
                count += 1
    
                # 环境状态塑形为一行,
                s = np.reshape(s, [1, INPUT])
    
                Q = sess.run(Q_pre, feed_dict={x: s})
    
                if e > np.random.rand(1):
                    a = env.action_space.sample()
                else:
                    a = np.argmax(Q)
    
                s1, r, d, _ = env.step(a)
                #填充经验池
                REPLAY_MEMORY.append([s, a, r, s1, d])
    
                #经验池数量超过50000则更新
                if len(REPLAY_MEMORY) > 50000:
                    del REPLAY_MEMORY[0]
    
                rall += r
    
                s = s1
            #每10个episode更新目标Q函数的参数
            if episode % 10 == 1 and len(REPLAY_MEMORY) > 50:
    
                for sample in ran.sample(REPLAY_MEMORY, MINIBATCH):
                    s_r, a_r, r_r, s1_r, d_r = sample
    
                    Q = sess.run(Q_pre, feed_dict={x: s_r})
    
                    if d_r:
                        Q[0, a_r] = r_r
                    else:
                        s1_r = np.reshape(s1_r, [1, INPUT])
                        Q1 = sess.run(Q_pre_r, feed_dict={x: s1_r})
                        Q[0, a_r] = r_r + DISCOUNT * np.max(Q1)
                        
                    _, loss = sess.run([train, cost], feed_dict={x: s_r, y: Q})
    
                sess.run(replace_target_para)
                
                print('loss:{} '.format(loss))
    
            recent_rlist.append(rall)
    
            print("Episode%d: reward:%.2f recent reward:%.4f" %(episode, rall,np.mean(recent_rlist)))
        #保存模型
        saver = tf.train.Saver()
        save_path = saver.save(sess, model_path)
        print("Model saved in file: ", save_path)

相关文章

网友评论

      本文标题:tensorflow 练习

      本文链接:https://www.haomeiwen.com/subject/bbfzzktx.html