循环神经网络
Posted hanouba
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环神经网络相关的知识,希望对你有一定的参考价值。
import numpy as np import tensorflow as tf from sklearn.datasets import load_iris BATCH_SIZE = 8 COST = 9 PROFIT = 1 iris = load_iris() iris_data = iris.data iris_label = iris.target iris_label = np.reshape(iris_label,(150,1))
## 在tensorflow中数据类型不同不能进行运算 iris_label = tf.to_double(iris_label,name = "ToDouble") ## training data and label x = iris_data length = len(x) x = tf.to_double(iris_label,name="ToDouble") #y_ = tf.placeholder(tf.float32,shape=(None,1)) ## Variables state = tf.Variable(tf.random_normal([4,4],stddev=1,dtype=tf.float64,seed=1)) #print(state.dtype) w_cell_input = tf.Variable(tf.random_normal([1,4],stddev=1,dtype=tf.float64,seed=1)) w_cell_state = tf.Variable(tf.random_normal([4,4],stddev=1,dtype=tf.float64,seed=1)) w_output = tf.Variable(tf.random_normal([4,4],stddev=1,dtype=tf.float64,seed=1)) b_output = tf.Variable(tf.random_normal([4,4],stddev=1,dtype=tf.float64,seed=1)) b_cell = tf.Variable(tf.random_normal([4,4],stddev=1,dtype=tf.float64,seed=1)) w1_reshape = tf.Variable(tf.random_normal([4,1],stddev=1,dtype=tf.float64,seed=1)) w2_reshape = tf.Variable(tf.random_normal([1,4],stddev=1,dtype=tf.float64,seed=1)) for i in range(length): #x[i] = np.reshape(x[i],(4,1)) print(x[i].dtype) before_activation = np.dot(state,w_cell_state) + x[i]*w_cell_input + b_cell state = tf.nn.tanh(before_activation) final_output = np.dot(state,w_output) + b_output temp = tf.matmul(final_output,w1_reshape) output = tf.matmul(w2_reshape,temp) loss = tf.reduce_sum(tf.where(tf.greater(iris_label[i],output),(iris_label[i]-output)*COST, (output-iris_label[i]*PROFIT))) train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) steps = 5000 for k in range(steps): start = (i * BATCH_SIZE) % 32 end = (i * BATCH_SIZE) % 32 + BATCH_SIZE sess.run(train_step) if k%1000 == 0: print(sess.run(loss))
tensorflow中数据类型的转换函数:
#########################################################################
字符串 ==> 数字
tf.string_to_number(string , out_type = None , name = None)
#########################################################################
==>float64
tf.to_double(x , name = "ToDouble")
#########################################################################
==>float32
tf.to_float(x , name = "Tofloat")
#########################################################################
==>int32
tf.to_int32(x , name = "ToInt32")
#########################################################################
==>int64
tf.to_int64(x , name = "ToInt64")
#########################################################################
==>将 x 转换为 dtype
tf.case( a ,tf.int32)
以上是关于循环神经网络的主要内容,如果未能解决你的问题,请参考以下文章