Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 12.1
- 42.2
- 32.2
- 15.2
- # -*- coding: utf-8 -*-
- import tensorflow as tf #NN用ライブラリであるtensorflowをインポート
- import numpy as np #計算用ライブラリであるnumpyをインポート
- '''----------------------モデル定義---------------------------------'''
- #入力層のユニット数
- num_units1 = 10 #中間層1のユニット数
- x=tf.placeholder(tf.float32,[1, 720]) #入力層
- w_enc = tf.Variable(tf.random_normal([720, num_units1], stddev=0.01)) #入力側の重み
- b_enc = tf.Variable(tf.zeros([num_units1])) #入力側のバイアス
- w_dec = tf.Variable(tf.random_normal([num_units1,720], stddev=0.01)) #出力側の重み
- b_dec = tf.Variable(tf.zeros([720])) #出力側のバイアス
- encoded = tf.nn.relu(tf.matmul(x, w_enc) + b_enc) #入力側活性化関数(ReLU)
- #drop=tf.nn.dropout(encoded,0.5) #dropout
- decoded = tf.matmul(encoded, w_dec) + b_dec #出力側活性化関数(恒等写像)
- lambda2 = 0.1 #正則化係数
- l2_norm = tf.nn.l2_loss(w_enc) + tf.nn.l2_loss(w_dec) #L2正則化
- loss1 = tf.reduce_sum(tf.square(x - decoded)) + lambda2 * l2_norm #損失関数(二乗誤差関数)
- loss2= tf.abs(tf.reduce_sum(decoded - x) / tf.reduce_sum(x)) * 100 #百分率で表した誤差
- train_step = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss1) #勾配降下法(Adam)
- '''--------------変数の初期化とVLFデータの読み込み---------------'''
- sess = tf.InteractiveSession() #セッション開始を変数に定義
- sess.run(tf.global_variables_initializer()) #変数初期化
- a = np.loadtxt("train_datasin125.txt") #訓練データを読み込む
- b = np.loadtxt("sintest_data11.txt") #テストデータ1を読み込む
- c = np.loadtxt("sintast_data2.txt") #テストデータ2を読み込む
- '''-------------------------学習開始---------------------------'''
- for step in range(1000000): #学習回数
- r = a[np.random.randint(0, 2000, [1])] #訓練データの中からランダムに1日分のデータを選出
- sess.run(train_step, feed_dict={x:r}) #学習セッション開始
- if step%49==0: #50回学習するごとに
- loss_val = sess.run(loss2, feed_dict={x:r}) #百分率で表した誤差を算出
- print('Step: %d, Loss: %f'%(step,loss_val)) #現在の学習回数と誤差を表示
- if step%499==0: #学習回数500回ごとに
- '''-------------------現在の学習回数を格納---------------------'''
- if step==0:
- out_g=step+1
- else:
- out_g=np.column_stack([out_g,step+1])
- '''------------------教師データの最大誤差算出------------------'''
- max = 0
- for i in range(2000):
- r = a[i, :]
- r = r.reshape(1, 720)
- loss_val = sess.run(loss2, feed_dict={x: r})
- if max<loss_val:
- max=loss_val
- if i==1999:
- if step == 0:
- out1 = max
- else:
- out1 = np.column_stack([out1, max])
- '''--------------テストデータ1の最大誤差算出------------------'''
- max = 0
- for q in range(30):
- r = c[q, :]
- r = r.reshape(1, 720)
- loss_val = sess.run(loss2, feed_dict={x: r})
- if max < loss_val:
- max = loss_val
- if q == 29:
- if step == 0:
- out3 = max
- else:
- out3 = np.column_stack([out3, max])
- '''--------テストデータ2の最大誤差と推定日数の算出--------'''
- max = out3
- count=0
- for p in range(48):
- r = b[p, :]
- r = r.reshape(1, 720)
- loss_val = sess.run(loss2, feed_dict={x: r})
- if max<loss_val:
- count=count+1
- if p==47:
- if step == 0:
- out2 = max
- out4=count
- else:
- out2 = np.column_stack([out2, max])
- out4 = np.column_stack([out4, count])
- '''------------------テキストファイルを出力--------------------'''
- out=np.column_stack([out_g.T,out1.T,out2.T,out3.T])
- np.savetxt("sinoutput110.txt",out)
- out=np.column_stack([out4.T])
- np.savetxt("sinoutput010.txt",out)
- /Users/ishidatakuma/PycharmProjects/sotsuken/venv/bin/python /Users/ishidatakuma/PycharmProjects/sotsuken/jikkenn2.py
- Step: 0, Loss: 147.256897
- Step: 49, Loss: 2897.626221
- Step: 98, Loss: 18581.408203
- Step: 147, Loss: 33576.679688
- Step: 196, Loss: 36586.777344
- Step: 245, Loss: 20837.001953
- Step: 294, Loss: 101844.562500
- Step: 343, Loss: 16776.875000
- Step: 392, Loss: 5594.307129
- Step: 441, Loss: 20111.017578
- Step: 490, Loss: 5526.880859
- Traceback (most recent call last):
- File "/Users/ishidatakuma/PycharmProjects/sotsuken/jikkenn2.py", line 107, in <module>
- if yosino<loss_val:
- ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Add Comment
Please, Sign In to add comment