質問編集履歴

1 コード修正しました

zakio49

zakio49 score 29

2017/07/09 20:20  投稿

tensorflow csvのデータ形式
tensorflow shapeの整え方をお尋ねしたいです。csvのデータ形式
tensorflowで12個の要素から学習を行い、restoreを行い実践データをセットした際に1×12の行列ではなくて、shape[1]のスカラー値を要求されエラーになります。shape[1]という要求は読み込みデータを
今のcsv
rank 1 shape [1,12]  
````
[ 0.71428573, 0.85714287, 0.71428573, 0.5714286 , 0.5714286 ,
       0.71428573, 0.5714286 , 0.71428573, 0.71428573, 0.71428573,
       0.5714286 , 0.71428573]
````
こう変える
こう変えることでshape[12,1,1] shape[1]という要求を満たせるですか?
お知恵を貸してください!
````
[ [0.71428573], [0.85714287], [0.71428573], [0.5714286] , [0.5714286] ,
       [0.71428573], [0.5714286] , [0.71428573], [0.71428573], [0.71428573],
       [0.5714286] , [0.71428573]]
````
````
ValueError: Argument must be a dense tensor: [array([ 0.71428573, 0.85714287, 0.71428573, 0.5714286 , 0.5714286 ,
       0.71428573, 0.5714286 , 0.71428573, 0.71428573, 0.71428573,
       0.5714286 , 0.71428573], dtype=float32)] - got shape [1, 12], but wanted [1].
````
````
その他のコード(問題なのはsession2のほうです)
```
import tensorflow as tf
import numpy
import os
cwd = os.getcwd()
SCORE_SIZE = 12
HIDDEN_UNIT_SIZE = 40
TRAIN_DATA_SIZE = 45
TACK = 1
raw_input = numpy.loadtxt(open("test.csv"), delimiter=",")
[tensor, score] = numpy.hsplit(raw_input, [1])
[tensor_train, tensor_test] = numpy.vsplit(tensor, [TRAIN_DATA_SIZE])
[score_train, score_test] = numpy.vsplit(score, [TRAIN_DATA_SIZE])
print(score_test)
#tensorは正解データtrainは学習モデル、scoreは学習データ、testは実データ
def inference(score_placeholder):
 with tf.name_scope('hidden1') as scope:
   hidden1_weight = tf.Variable(tf.truncated_normal([SCORE_SIZE, HIDDEN_UNIT_SIZE], stddev=0.01), name="hidden1_weight")
   hidden1_bias = tf.Variable(tf.constant(0.1, shape=[HIDDEN_UNIT_SIZE]), name="hidden1_bias")
   hidden1_output = tf.nn.relu(tf.matmul(score_placeholder, hidden1_weight) + hidden1_bias)
 with tf.name_scope('output') as scope:
   output_weight = tf.Variable(tf.truncated_normal([HIDDEN_UNIT_SIZE, 1], stddev=0.01), name="output_weight")
   output_bias = tf.Variable(tf.constant(0.1, shape=[1]), name="output_bias")
   output = tf.matmul(hidden1_output, output_weight) + output_bias
 if TACK != 1:
   print("saku1")
   print(output)
 else:
   print("saku2")
   
   return tf.nn.l2_normalize(output, 0)
def loss(output, tensor_placeholder, loss_label_placeholder):
 with tf.name_scope('loss') as scope:
   loss = tf.nn.l2_loss(output - tf.nn.l2_normalize(tensor_placeholder, 0))
   tf.summary.scalar('loss_label_placeholder', loss)
 return loss
def training(loss):
 with tf.name_scope('training') as scope:
   train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
 return train_step
with tf.Graph().as_default():
 tensor_placeholder = tf.placeholder(tf.float32, [None, 1], name="tensor_placeholder")
 score_placeholder = tf.placeholder(tf.float32, [None, SCORE_SIZE], name="score_placeholder")
 loss_label_placeholder = tf.placeholder("string", name="loss_label_placeholder")
 feed_dict_train={
   tensor_placeholder: tensor_train,
   score_placeholder: score_train,
   loss_label_placeholder: "loss_train"
 }
 feed_dict_test={
   tensor_placeholder: tensor_test,
   score_placeholder: score_test,
   loss_label_placeholder: "loss_test"
 }
 output = inference(score_placeholder)
 loss = loss(output, tensor_placeholder, loss_label_placeholder)
 training_op = training(loss)
 summary_op = tf.summary.merge_all()
 init = tf.global_variables_initializer()
 best_loss = float("inf")
 with tf.Session() as sess:
   summary_writer = tf.summary.FileWriter('data', graph_def=sess.graph_def)
   sess.run(init)
   for step in range(10000):
     sess.run(training_op, feed_dict=feed_dict_train)
     loss_test = sess.run(loss, feed_dict=feed_dict_test)
     if loss_test < best_loss:
       best_loss = loss_test
       best_match = sess.run(output, feed_dict=feed_dict_test)
     #if step % 100 == 0:
     # summary_str = sess.run(summary_op, feed_dict=feed_dict_test)
     # summary_str += sess.run(summary_op, feed_dict=feed_dict_train)
     # summary_writer.add_summary(summary_str, step)
   
   saver=tf.train.Saver() 
   saver.save(sess,cwd+'/model.ckpt')
   print(cwd)
   print(best_match) 
   print('Saved a model.')
   sess.close()
 with tf.Session() as sess2:
 #変数の読み込み
   summary_writer = tf.summary.FileWriter('data', graph=sess2.graph)
   #sess2.run(init)
 #新しいデータ
   TRAIN_DATA_SIZE2 = 0
   test2 = numpy.loadtxt(open("one_record.csv"), delimiter=",").astype(numpy.float32)
   score3 = [test2]
   print(score3)
   saver = tf.train.Saver()
   cwd = os.getcwd()
   saver.restore(sess2,cwd + "/model.ckpt")
   best_match2 = sess2.run(inference(score3))
   print(best_match2)
   print("fin")
   sess2.close()
```
  • Python

    21034 questions

    Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る