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

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

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

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

6631閲覧

TensorFlow2.0で消滅したplaceholderの代わりになるものがわからない

wasshoikimura

総合スコア10

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/11/18 03:26

編集2019/11/19 08:28

今TensorFlow公式のチュートリアルを実行しているのですが、自分が使っているTensorFlow2.0には存在しない「placeholder」が使われており、進みません。

発生している問題・エラーメッセージ


AttributeError Traceback (most recent call last)
<ipython-input-14-160ddb0d1902> in <module>
----> 1 x = tf.placeholder(tf.zeros([None,784]))

AttributeError: module 'tensorflow' has no attribute 'placeholder'

該当のソースコード

linux ubuntu18.04.3 Python3.6.8-TensorFlow2.0-gpu NVIDIA-SMI 430.50 Driver Version: 430.50 CUDA Version: 10.1 `` tf.TensorSpecを試してみましたが以下のようなエラーが出 import tensorflow as tf from tensorflow import keras ​from matplotlib import pyplot as plt from matplotlib import cm import numpy as np (trainX,trainY),(testX,testY) = keras.datasets.mnist.load_data() w = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) x = tf.TensorSpec([None], tf.float32) y = tf.nn.softmax(tf.matmul(x, w)+b) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-17-9ff920be21e1> in <module> ----> 1 y = tf.nn.softmax(tf.matmul(x, w)+b) ~/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/util/dispatch.py in wrapper(*args, **kwargs) 178 """Call target, and fall back on dispatchers if there is a TypeError.""" 179 try: --> 180 return target(*args, **kwargs) 181 except (TypeError, ValueError): 182 # Note: convert_to_eager_tensor currently raises a ValueError, not a ~/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/ops/math_ops.py in matmul(a, b, transpose_a, transpose_b, adjoint_a, adjoint_b, a_is_sparse, b_is_sparse, name) 2693 if context.executing_eagerly(): 2694 if not isinstance(a, (ops.EagerTensor, _resource_variable_type)): -> 2695 a = ops.convert_to_tensor(a, name="a") 2696 if not isinstance(b, (ops.EagerTensor, _resource_variable_type)): 2697 b = ops.convert_to_tensor(b, name="b") ~/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype, dtype_hint) 1182 preferred_dtype = deprecation.deprecated_argument_lookup( 1183 "dtype_hint", dtype_hint, "preferred_dtype", preferred_dtype) -> 1184 return convert_to_tensor_v2(value, dtype, preferred_dtype, name) 1185 1186 ~/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name) 1240 name=name, 1241 preferred_dtype=dtype_hint, -> 1242 as_ref=False) 1243 1244 ~/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_composite_tensors) 1294 1295 if ret is None: -> 1296 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 1297 1298 if ret is NotImplemented: ~/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref) 284 as_ref=False): 285 _ = as_ref --> 286 return constant(v, dtype=dtype, name=name) 287 288 ~/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py in constant(value, dtype, shape, name) 225 """ 226 return _constant_impl(value, dtype, shape, name, verify_shape=False, --> 227 allow_broadcast=True) 228 229 ~/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast) 233 ctx = context.context() 234 if ctx.executing_eagerly(): --> 235 t = convert_to_eager_tensor(value, ctx, dtype) 236 if shape is None: 237 return t ~/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype) 94 dtype = dtypes.as_dtype(dtype).as_datatype_enum 95 ctx.ensure_initialized() ---> 96 return ops.EagerTensor(value, ctx.device_name, dtype) 97 98 ValueError: Attempt to convert a value (TensorSpec(shape=(None,), dtype=tf.float32, name=None)) with an unsupported type (<class 'tensorflow.python.framework.tensor_spec.TensorSpec'>) to a Tensor. ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

以下のようにして numpy 配列をそのまま渡せます。

python

1import numpy as np 2import tensorflow as tf 3 4(trainX, trainY), (testX, testY) = tf.keras.datasets.mnist.load_data() 5 6 7W = tf.Variable(tf.zeros([784, 10])) 8b = tf.Variable(tf.zeros([10])) 9 10 11@tf.function 12def forward(x): 13 return tf.nn.softmax(x @ W + b) 14 15 16x = np.random.randn(10, 784).astype(np.float32) 17forward(x)

今TensorFlow公式のチュートリアルを実行しているのですが、自分が使っているTensorFlow2.0には存在しない「placeholder」が使われており、進みません。

tensorflow 2.0 で API が大幅に変更されて、Web や書籍のコードは自分で修正しないと動かない状態なので、
チュートリアル通りにやりたい場合はしばらくは 1.14 あたりを使ったほうがいいと思います。

python

1pip uninstall tensorflow -y 2pip install tensorflow==1.14

投稿2019/11/20 04:13

編集2019/11/20 04:16
tiitoi

総合スコア21956

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

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

wasshoikimura

2019/11/20 04:21

回答ありがとうございます。 初心者でweb,書籍のコードを参考にしたいので、あきらめてバージョンを1.13に下げて出直します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問