关于一个7位数之类的加法,用普通的rnn算法实现

import numpy as np
import tensorflow as tf
from sklearn.preprocessing import OneHotEncoder
import random
import os
from tensorflow.contrib import rnn
data_num=np.array([0,1,2,3,4,5,6,7,8,9])
data_num=data_num.reshape([-1,1])
data_num=OneHotEncoder().fit_transform(data_num).todense()
batch_size=100
#print(data)
def get_data(batch_size=100):
    batch_in_data=[]
    batch_out_data=[]
    for i in range(batch_size):
        a=random.randint(0,10000000)
        b=random.randint(0,10000000)
        c=[a,b]
        d=a+b
        
        d1=d
        
        data=[]
        label=[]
        out_data=[]
        for j in c:
            for i in range(8):
                data.insert(i,j%10)
                j=j//10
        
        data=np.array(data)
        data=data.reshape([2,8]).T
        data=data.reshape([8,2])

        for i in range(8):
                out_data.insert(i,d%10)
                d=d//10      
        for x in out_data:
            label.append(data_num[x])
            
        #print(out_data)
        label=np.array(label)
        label=label.reshape([8,10])
        
        batch_out_data.append(label) 
        batch_in_data.append(data)
        
    batch_in_data=np.array(batch_in_data)
    batch_out_data=np.array(batch_out_data)
    batch_in_data=batch_in_data.astype("float32")
    batch_out_data=batch_out_data.astype("float32")
    batch_in_data=batch_in_data.reshape([-1,8,2])
    batch_out_data=batch_out_data.reshape([-1,8,10])
    return batch_in_data,batch_out_data,d1
#输入:
x=tf.placeholder(tf.float32,[None,8,2])
y=tf.placeholder(tf.float32,[None,8,10])
#y=tf.placeholder(tf.int64,[8])
weights = {
    'out' : tf.Variable(tf.random_normal([8, 10])) #128*10
}

biases = {
    'out' : tf.Variable(tf.random_normal([10])) #1*10
}

weights = {'in':tf.Variable(tf.random_normal([2, 16])),  
           'out':tf.Variable(tf.random_normal([8, 10]))}  
  
biases = {'in':tf.Variable(tf.constant(0.1, shape=[16, ])),  
           'out':tf.Variable(tf.constant(0.1, shape=[10, ]))}
'''
def RNN(X, weights, biases):
    #print(X)
    X=tf.reshape(X,[-1,2])
    X_in = tf.matmul(X, weights['in']) + biases['in']
    #print(X_in)
    X_in = tf.reshape(X_in, [8, -1,16])
    #print(X_in)
    # 使用基本的LSTM循环网络单元  
    #lstm_cell = tf.contrib.rnn.BasicLSTMCell(16, forget_bias=1.0, state_is_tuple=True)
    lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(8, forget_bias=1.0, state_is_tuple=True)

    # 初始化为0,LSTM单元由两部分构成(c_state, h_state)  
    init_state = lstm_cell.zero_state(8, dtype=tf.float32)

    # dynamic_rnn接收张量要么为(batch, steps, inputs)或者(steps, batch, inputs)作为X_in  
    outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, X_in, initial_state=init_state, time_major=False)
    print("output:",outputs)
    print("final_state:",final_state)
    results = tf.matmul(final_state[1], weights['out']) + biases['out']
    #outputs = tf.reshape(outputs, [-1, 8])
    #results = tf.matmul(outputs, weights['out']) + biases['out']
    #results = tf.reshape(results, [8, -1, 10])
    print("results:",results)
    #results = tf.transpose(results, [1, 0,2])
    #results = tf.reshape(results, [-1, 10])
    return results
'''
def RNN(X, weights, biases):
    x=tf.transpose(X,[1,0,2])
    x=tf.reshape(x,[-1,2])
    x = tf.split(x, 8,0)
    print("x:",x)
    rnn_cell = tf.nn.rnn_cell.BasicRNNCell(8)
    outputs, states = rnn.static_rnn(rnn_cell, x, dtype=tf.float32)
    with tf.variable_scope('hidden_to_output_layer'):
        w2out = tf.get_variable('w2out', shape=[8, 10], dtype=tf.float32, initializer=tf.truncated_normal_initializer(0.0, 1.0))
        bias2out = tf.get_variable('bias2oput', shape=[10], dtype=tf.float32, initializer=tf.constant_initializer(0.0))
    print("outputs:",outputs)
    outputs = tf.reshape(outputs, [-1, 8])
    print("outputs:",outputs)
    #result = tf.nn.sigmoid(tf.matmul(outputs, w2out)+bias2out)
    result = tf.matmul(outputs, w2out)+bias2out
    print(result)
    result = tf.reshape(result, [8, -1, 10])
    print(result)
    result = tf.transpose(result, [1, 0,2])
    print(result)
    return result

'''
def RNN(x, weights, biase):
    #print(1)
    x = tf.unstack(x, 8, 1) #分解维数
    #print(len(x))
    lstm_cell = rnn.BasicLSTMCell(8, forget_bias=1.0) #LSTM最简单的神经元 128
    outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32) #
    print("ouyputs:",outputs)
    return tf.nn.softmax(tf.matmul(outputs[-1], weights['out']) + biases['out'])#矩阵相乘
'''
pred = RNN(x, weights, biases)

pre = tf.argmax(pred,2)
#pre = tf.argmax(pred,1)
#cost = tf.reduce_mean(tf.square(pred-y))
#print(pre)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=y))
#optimizer = tf.train.AdagradOptimizer(1).minimize(cost)
optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)

correct_pred = tf.equal(tf.argmax(pred, 2), tf.argmax(y, 2))

accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

def train():
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(2000000):
            batch_xs,batch_ys,d=get_data()
            #print(batch_xs.shape,batch_ys.shape)

            sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
            if i%100 ==0:
                cc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys})
                loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})
                print("cc:",cc," loss:",loss)
            if i % 1000==0:
                batch_xs,batch_ys,d=get_data(1)
                #print(d)
                #print(batch_xs)
                #print(batch_ys)
                #os.system("pause")
                pree = sess.run(pre,feed_dict={x:batch_xs})
                cc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys})
                loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})
                print("true:",d)
                print("pree:",pree[0][::-1][0:8])
                #print("pree1:",pree)
                print("ac:",cc)
                print("loss:",loss)
                print("________________")
        sess.close()

train()
最后的运行图:



猜你喜欢

转载自blog.csdn.net/by_side_with_sun/article/details/80267854