码迷,mamicode.com
首页 > 其他好文 > 详细

循环神经网络

时间:2019-09-23 15:07:14      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:end   art   print   fit   double   seed   for   nbsp   desc   

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)

 

循环神经网络

标签:end   art   print   fit   double   seed   for   nbsp   desc   

原文地址:https://www.cnblogs.com/hanouba/p/11572131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!