質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

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

Q&A

解決済

1回答

3103閲覧

tensorflow shapeの整え方をお尋ねしたいです。csvのデータ形式

zakio49

総合スコア29

Python

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

0グッド

0クリップ

投稿2017/07/09 11:13

編集2017/07/09 11:20

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()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

shape を調整してあげる必要があるかもですが、、、
こんな感じでしょうね。

Python

1# best_match2 = sess2.run(inference(score3)) 2best_match2 = sess2.run(output, feed_dict={score_placeholder: test2})

投稿2017/07/09 11:27

MasashiKimura

総合スコア1150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zakio49

2017/07/12 02:34

output = inference(score_placeholder)に一度いれるとinference()の返り値がoutputに入るので、session内ではinferenceが実行されていないことがわかりました。 (output, feed_dict={score_placeholder:[test2]})の記法でsessionを始めないといけないみたいですね。 うまく実行することができました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問