<1/26追記>
最下部の内容から進展があったので、記事を更新します。
問題があるであろう、グラフ作成の部分と実行部分を抜粋します(いくつか変更してます)。
実行部ではfor文でエポック数を増やしたいのですが、2周目からエラーが出るようです。実行部のfor文をfor i in range(1)とした場合はエラーは生じません。
エラー内容は「sess.runにndarrayを入れるな。テンソルを入れてくれ」ということだと思いますが、何故、forの1周目でテンソルだったものが2周目からndarrayに変化してしまうのかがわかりません。
python
1グラフの作成 2g = tf.Graph() 3with g.as_default(): 4 tf.set_random_seed(123) 5 6 tf_x = tf.placeholder(tf.float32, shape=(None, 4), name='tf_x') 7 tf_y = tf.placeholder(tf.int32, shape=(None), name='tf_y') 8 9 oh_y = tf.one_hot(tf_y, 3, dtype=tf.float32, name='oh_y') 10 11 w = tf.Variable(tf.random_normal((4, 3)), name='weight') 12 b = tf.Variable(tf.zeros(3), name='bias') 13 14 logits = tf.add(tf.matmul(tf_x, w), b, name='logits') 15 16 prediction = {'probabilities': tf.nn.softmax(logits, name='probabilities'), 17 'labels': tf.argmax(logits,1)} 18 19 cost = tf.losses.softmax_cross_entropy(logits=logits, onehot_labels=oh_y) 20# cost = -tf.reduce_sum(oh_y * tf.log(prediction['probabilities'])) 21 optimizer = tf.train.AdamOptimizer() 22 train = optimizer.minimize(cost) 23 correct_predictions = tf.equal(prediction['labels'], tf.argmax(oh_y, 1)) 24# tf.reduce_meanの中身はfloatにしてあげないと小数点の計算できない 25 accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32), name='accuracy') 26 init = tf.global_variables_initializer() 27 28実行 29with tf.Session(graph=g) as sess: 30 sess.run(init) 31# ↓のfor文のrange(1)にすると、エラーが生じません。 32 for step in range(2): 33 _, cost= sess.run([train, cost], feed_dict={tf_x: X_train, tf_y: y_train}) 34 if (step + 1) % 10 == 0: 35 print('Epoch%2d Cost:%.2f' %(step+1, cost)) 36 37 print('Prediction Accuracy: %.2f' %(sess.run(accuracy, feed_dict={tf_x: X_test, tf_y: y_test}) * 100)) 38
生じているエラー
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __init__(self, fetches, contraction_fn) 299 self._unique_fetches.append(ops.get_default_graph().as_graph_element( --> 300 fetch, allow_tensor=True, allow_operation=True)) 301 except TypeError as e: /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in as_graph_element(self, obj, allow_tensor, allow_operation) 3489 with self._lock: -> 3490 return self._as_graph_element_locked(obj, allow_tensor, allow_operation) 3491 /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation) 3578 raise TypeError("Can not convert a %s into a %s." % (type(obj).__name__, -> 3579 types_str)) 3580 TypeError: Can not convert a float32 into a Tensor or Operation. During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipython-input-134-171c87a28859> in <module>() 3 # ↓のfor文のrange(1)にすると、エラーが生じません。 4 for step in range(2): ----> 5 _, cost= sess.run([train, cost], feed_dict={tf_x: X_train, tf_y: y_train}) 6 if (step + 1) % 10 == 0: 7 print('Epoch%2d Cost:%.2f' %(step+1, cost)) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 927 try: 928 result = self._run(None, fetches, feed_dict, options_ptr, --> 929 run_metadata_ptr) 930 if run_metadata: 931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 1135 # Create a fetch handler to take care of the structure of fetches. 1136 fetch_handler = _FetchHandler( -> 1137 self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles) 1138 1139 # Run request and get response. /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __init__(self, graph, fetches, feeds, feed_handles) 469 """ 470 with graph.as_default(): --> 471 self._fetch_mapper = _FetchMapper.for_fetch(fetches) 472 self._fetches = [] 473 self._targets = [] /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in for_fetch(fetch) 259 elif isinstance(fetch, (list, tuple)): 260 # NOTE(touts): This is also the code path for namedtuples. --> 261 return _ListFetchMapper(fetch) 262 elif isinstance(fetch, collections.Mapping): 263 return _DictFetchMapper(fetch) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __init__(self, fetches) 368 """ 369 self._fetch_type = type(fetches) --> 370 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 371 self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 372 /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in <listcomp>(.0) 368 """ 369 self._fetch_type = type(fetches) --> 370 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 371 self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 372 /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in for_fetch(fetch) 269 if isinstance(fetch, tensor_type): 270 fetches, contraction_fn = fetch_fn(fetch) --> 271 return _ElementFetchMapper(fetches, contraction_fn) 272 # Did not find anything. 273 raise TypeError('Fetch argument %r has invalid type %r' % (fetch, /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __init__(self, fetches, contraction_fn) 302 raise TypeError('Fetch argument %r has invalid type %r, ' 303 'must be a string or Tensor. (%s)' % --> 304 (fetch, type(fetch), str(e))) 305 except ValueError as e: 306 raise ValueError('Fetch argument %r cannot be interpreted as a ' TypeError: Fetch argument 5.690171 has invalid type <class 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)
<以下、更新前の原文>
GitHubの記事を参考にTensorFlowでアヤメの分類問題をやっているのですが、恐らくデータ型のエラーがどうしても解消できません。
コスト関数の最適化のとこでエラーが出ていますが、上の記事と見合わせても、データ型的にどこが間違えているのかがわかりません。
解決の方法がわかる方、是非ご教授くださると助かります。
試行錯誤して色々書き換えると様々なエラーが生じるので、もしかしたら多くの個所がおかしいのかもしれません。
書いたコード
python
1from urllib.request import urlretrieve 2import pandas as pd 3import numpy as np 4import tensorflow as tf 5from sklearn.model_selection import train_test_split 6 7# データの呼び出し 8url = '上記記事のコードのURL' 9urlretrieve(url, 'iris.csv') 10df = pd.read_csv('iris.csv', encoding='utf-8') 11 12# データの準備 13names = sorted(set(df.Name.values)) 14name2num = {w:i for i, w in enumerate(names)} 15df['label'] = df['Name'].map(name2num) 16X = df.iloc[:,:4].values 17y = df.label.values 18X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=0) 19 20 21# グラフの作成 22g = tf.Graph() 23with g.as_default(): 24 tf.set_random_seed(123) 25 26 tf_x = tf.placeholder(tf.float32, shape=(None, 4), name='tf_x') 27 tf_y = tf.placeholder(tf.int32, shape=(None), name='tf_y') 28 29 oh_y = tf.one_hot(tf_y, 3, dtype=tf.float32, name='oh_y') 30 31 w = tf.Variable(tf.random_normal((4, 3)), name='weight') 32 b = tf.Variable(tf.zeros(3), name='bias') 33 34 logits = tf.add(tf.matmul(tf_x, w), b, name='logits') 35 36 prediction = {'probabilities': tf.nn.softmax(logits, name='probabilities'), 37 'labels': tf.cast(tf.argmax(logits,1), tf.int32)} 38 39# cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=oh_y), name='cost') 40 cost = -tf.reduce_sum(oh_y * tf.log(prediction['probabilities'])) 41 optimizer = tf.train.AdamOptimizer() 42 train = optimizer.minimize(cost) 43 correct_predictions = tf.equal(prediction['labels'], tf.cast(tf.argmax(oh_y, 1), tf.int32)) 44 accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32), name='accuracy') 45 init = tf.global_variables_initializer() 46 47 48# 実行 49with tf.Session(graph=g) as sess: 50 sess.run(init) 51 52 for step in range(300): 53 _, cost, accuracy = sess.run([train, cost, accuracy], feed_dict={tf_x: X_train, tf_y: y_train}) 54 if (step + 1) % 10 == 0: 55 print('Epoch%2d Cost:%.2f Accuracy:%.2f%%' %(step+1, cost, accuracy*100)) 56 57 print('Prediction Accuracy: %.2f' %(sess.run(accuracy, feed_dict={tf_x: X_test, tf_y: y_test}) * 100))
生じているエラー(追記時に字数が足りなくなったので削りました)
--------------------------------------------------------------------------- TypeError: Fetch argument 682.8205 has invalid type <class 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)
環境など
Windows 10
Python 3.6.5
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/31 08:02