여기 첫번째 댓글에 보면 텐서플로우 버전2에서 작동하시는 분들은 import tensorflow as tf 대신에 import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
를 치고 하시면 잘 됩니다.
라고해서 구글 코랩에
import tensorflow.compat.v1 as tf
tf.set_random_seed(777) # for reproducibilitywith tf.compat.v1.Session() as sess:
x_train = [1,2,3]
y_train = [1,2,3] W = tf.Variable(tf.random_normal([1]), name=‘weight’)
b = tf.Variable(tf.random_normal([1]), name=‘bias’) hypothesis = x_train * W + b cost = tf.reduce_mean(tf.square(hypothesis - y_train)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost) sess = tf.Session()
#sess.run(tf.initialize_all_variables())
sess.run(tf.global_variables_initializer()) for step in range(2001):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(cost), sess.run(W), sess.run(b))
이렇게 하니까 잘 돌아감!
tensorflow git:(master) ✗ python3
Python 3.8.2 (default, Nov 4 2020, 21:23:28)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'2.4.0'
>>> hello = tf.constant("hello")
2021-01-17 13:53:19.755576: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-01-17 13:53:19.755949: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
>>> import tensorflow.combat.v1 as tf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow.combat'
>>> import tensorflow.compat.v1 as tf
>>> hello = tf.constant("hello")
>>> sess = tf.Session()
>>> print(sess.run(hello))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/selimmac/Library/Python/3.8/lib/python/site-packages/tensorflow/python/client/session.py", line 967, in run
result = self._run(None, fetches, feed_dict, options_ptr,
File "/Users/selimmac/Library/Python/3.8/lib/python/site-packages/tensorflow/python/client/session.py", line 1116, in _run
raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
>>> node1 = tf.constant(3.0, tf.float32)
>>> node2 = tf.constant(4.0)
>>> print(node1)
tf.Tensor(3.0, shape=(), dtype=float32)
>>> print(node2)
tf.Tensor(4.0, shape=(), dtype=float32)
>>> node3 = tf.add(node1, node2)
>>> print(node3)
tf.Tensor(7.0, shape=(), dtype=float32)
>>>
"""tensorflow mechanics
1. build graph using tensorflow operations
2. session run = feed data and run graph(operation)
sess.run(op)
3. update variables and return values
"""
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
#computational graph
#(1) build graph (tensors) using tensorflow operations
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)
node3 = tf.add(node1, node2)
#(2) feed data and run graph (operaton)
sess = tf.Session()
print(sess.run([node1, node2]))
print(sess.run(node3))
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b
print(sess.run(adder_node, feed_dict = {a : 3, b : 4.5}))
print(sess.run(adder_node, feed_dict = {a : [1,3], b : [2,4]}))
"""
[3.0, 4.0]
7.0
7.5
[3. 7.]
print(sess.run([node1, node2])
... )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/selimmac/Library/Python/3.8/lib/python/site-packages/tensorflow/python/client/session.py", line 967, in run
result = self._run(None, fetches, feed_dict, options_ptr,
File "/Users/selimmac/Library/Python/3.8/lib/python/site-packages/tensorflow/python/client/session.py", line 1116, in _run
raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
>>>
"""
# tensor array를 의미한다.
# rank 차원 shape 0 차원 1차원
# shape 모양 [] 로 이용해서 보여줌 [3, 3] 이면 이중 배열 3 * 3 개 있음
# type float~ int32 등 까지
'머신러닝,딥러닝 > tensorflow' 카테고리의 다른 글
lec08 deep neural network for everyone (0) | 2021.01.22 |
---|---|
lec07 learning rate, data preprocessing overfitting (0) | 2021.01.21 |
lec06 multinominal 개념 소개 (0) | 2021.01.21 |
lec05 logistic (regression) classification (0) | 2021.01.19 |
lec04 file읽어서 tf.model에 집어넣기 (0) | 2021.01.19 |
lec04 multi-variable linear regression tensorflow (0) | 2021.01.18 |
lec 03 linear regrssion 의 cost 최소화 (0) | 2021.01.18 |
tensorflow lec 02 linear (0) | 2021.01.17 |