25_Tensorflow_상수와변수
import warnings
warnings.filterwarnings('ignore')
!pip install tensorflow
Requirement already satisfied: tensorflow in c:\python\lib\site-packages (2.6.2)
Requirement already satisfied: tensorflow-estimator<2.7,>=2.6.0 in c:\python\lib\site-packages (from tensorflow) (2.6.0)
Requirement already satisfied: h5py~=3.1.0 in c:\python\lib\site-packages (from tensorflow) (3.1.0)
Requirement already satisfied: keras<2.7,>=2.6.0 in c:\python\lib\site-packages (from tensorflow) (2.6.0)
Requirement already satisfied: grpcio<2.0,>=1.37.0 in c:\python\lib\site-packages (from tensorflow) (1.41.1)
Requirement already satisfied: six~=1.15.0 in c:\python\lib\site-packages (from tensorflow) (1.15.0)
Requirement already satisfied: gast==0.4.0 in c:\python\lib\site-packages (from tensorflow) (0.4.0)
Requirement already satisfied: opt-einsum~=3.3.0 in c:\python\lib\site-packages (from tensorflow) (3.3.0)
Requirement already satisfied: protobuf>=3.9.2 in c:\python\lib\site-packages (from tensorflow) (3.19.1)
Requirement already satisfied: wheel~=0.35 in c:\python\lib\site-packages (from tensorflow) (0.37.0)
Requirement already satisfied: clang~=5.0 in c:\python\lib\site-packages (from tensorflow) (5.0)
Requirement already satisfied: flatbuffers~=1.12.0 in c:\python\lib\site-packages (from tensorflow) (1.12)
Requirement already satisfied: google-pasta~=0.2 in c:\python\lib\site-packages (from tensorflow) (0.2.0)
Requirement already satisfied: keras-preprocessing~=1.1.2 in c:\python\lib\site-packages (from tensorflow) (1.1.2)
Requirement already satisfied: numpy~=1.19.2 in c:\python\lib\site-packages (from tensorflow) (1.19.5)
Requirement already satisfied: wrapt~=1.12.1 in c:\python\lib\site-packages (from tensorflow) (1.12.1)
Requirement already satisfied: astunparse~=1.6.3 in c:\python\lib\site-packages (from tensorflow) (1.6.3)
Requirement already satisfied: tensorboard<2.7,>=2.6.0 in c:\python\lib\site-packages (from tensorflow) (2.6.0)
Requirement already satisfied: typing-extensions~=3.7.4 in c:\python\lib\site-packages (from tensorflow) (3.7.4.3)
Requirement already satisfied: absl-py~=0.10 in c:\python\lib\site-packages (from tensorflow) (0.15.0)
Requirement already satisfied: termcolor~=1.1.0 in c:\python\lib\site-packages (from tensorflow) (1.1.0)
Requirement already satisfied: cached-property in c:\python\lib\site-packages (from h5py~=3.1.0->tensorflow) (1.5.2)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in c:\python\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (1.8.0)
Requirement already satisfied: google-auth<2,>=1.6.3 in c:\python\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (1.35.0)
Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in c:\python\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (0.6.1)
Requirement already satisfied: requests<3,>=2.21.0 in c:\python\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (2.26.0)
Requirement already satisfied: setuptools>=41.0.0 in c:\python\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (58.5.3)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in c:\python\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (0.4.6)
Requirement already satisfied: werkzeug>=0.11.15 in c:\python\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (2.0.2)
Requirement already satisfied: markdown>=2.6.8 in c:\python\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (3.3.4)
Requirement already satisfied: rsa<5,>=3.1.4 in c:\python\lib\site-packages (from google-auth<2,>=1.6.3->tensorboard<2.7,>=2.6.0->tensorflow) (4.7.2)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in c:\python\lib\site-packages (from google-auth<2,>=1.6.3->tensorboard<2.7,>=2.6.0->tensorflow) (4.2.4)
Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\python\lib\site-packages (from google-auth<2,>=1.6.3->tensorboard<2.7,>=2.6.0->tensorflow) (0.2.8)
Requirement already satisfied: requests-oauthlib>=0.7.0 in c:\python\lib\site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.7,>=2.6.0->tensorflow) (1.3.0)
Requirement already satisfied: importlib-metadata in c:\python\lib\site-packages (from markdown>=2.6.8->tensorboard<2.7,>=2.6.0->tensorflow) (4.8.1)
Requirement already satisfied: charset-normalizer~=2.0.0 in c:\python\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.7,>=2.6.0->tensorflow) (2.0.7)
Requirement already satisfied: idna<4,>=2.5 in c:\python\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.7,>=2.6.0->tensorflow) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\python\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.7,>=2.6.0->tensorflow) (2021.10.8)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\python\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.7,>=2.6.0->tensorflow) (1.26.7)
Requirement already satisfied: dataclasses in c:\python\lib\site-packages (from werkzeug>=0.11.15->tensorboard<2.7,>=2.6.0->tensorflow) (0.8)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in c:\python\lib\site-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard<2.7,>=2.6.0->tensorflow) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in c:\python\lib\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.7,>=2.6.0->tensorflow) (3.1.1)
Requirement already satisfied: zipp>=0.5 in c:\python\lib\site-packages (from importlib-metadata->markdown>=2.6.8->tensorboard<2.7,>=2.6.0->tensorflow) (3.6.0)
# tensorflow 라이브러리를 설치하고 tf라는 별명을 지정한다.
# !pip install tensorflow
#!pip install tensorflow
#import tensorflow as tf # 2.x 버전 import
# tensorflow 2.x버전이 설치된 경우 tensorflow 1.x버전 문법을 사용하기 위해
# 아래와 같이 import한다
import tensorflow.compat.v1 as tf
# tensorflow 2.x버전의 문법을 사용하지 않켔다고 선언한다.
tf.disable_v2_behavior()
print(tf.__version__)
WARNING:tensorflow:From c:\python\lib\site-packages\tensorflow\python\compat\v2_compat.py:101: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
2.6.2
상수,수식을 만든다 => 세션을 만든다.=> 실행한다. tensorflow상수
# constant()함수로 tensorflow에서 사용할 상수를 정의한다.
a = tf.constant(300) #상수를 정의한다.
print(a)
b = tf.constant(500) #상수를 정의한다.
print(b)
c=a+b #수식을 정의한다.
print(c)
d = tf.add(a,b)
print(d)
#tensorflow는 반드시 Session이라는 작업 영역을
#만든 후 상수, 변수,수식을 대입한 다음 실행해야 한다.
sess = tf.Session() # Session을 만든다.
#run() 함수로 tensorflow에서 Session의 내용을 실행한다.
print('a : {}'.format(sess.run(a)))
print('b : {}'.format(sess.run(b)))
print('c : {}'.format(sess.run(c)))
print('d : {}'.format(sess.run(d)))
Tensor("Const_19:0", shape=(), dtype=int32)
Tensor("Const_20:0", shape=(), dtype=int32)
Tensor("add_10:0", shape=(), dtype=int32)
Tensor("Add_11:0", shape=(), dtype=int32)
a : 300
b : 500
c : 800
d : 800
변수를 만든다 => 세션을 만든다. => 변수를 초기화 시킨다. => 실행한다.
tensorflow변수
# Variable() 함수로 tensorflow에서 사용할 변수를 정의한다.
a = tf.Variable(5) #a는 5를 기억하는 변수를 정의한다.
print(a)
b = tf.Variable(3) #b는 5를 기억하는 변수를 정의한다.
print(b)
c=a*b
print(c)
session = tf.Session()
# Variable()함수를 사용해서 생성한 변수는 반드시
# 사용하기 전에 초기화시켜야 한다.
# 변수 초기화는 변수를 사용하기 전 에만 해주면 된다.
# tensorflow 변수 초기화
# global_variables_initializer() 함수로 tensorflow에서 사용할
# 변수를 초기화시킨다.
#init = tf.global_variables_initializer()
#sess.run(init)
#위의 두줄로 사용한 변수 초기화를 아래와 같이
#한줄로 줄여서 사용할 수 있다.
sess.run(tf.global_variables_initializer())
print('a : {}'.format(sess.run(a)))
print('b : {}'.format(sess.run(b)))
print('c : {}'.format(sess.run(c)))
a = tf.Variable(15) #변수에 저장된 데이터를 변경했다.
#변수에 저장된 데이터가 변경되었으므로, 다시 초기화한다.
sess.run(tf.global_variables_initializer())
d = tf.multiply(a, b)
print('d : {}'.format(sess.run(d)))
<tf.Variable 'Variable_12:0' shape=() dtype=int32_ref>
<tf.Variable 'Variable_13:0' shape=() dtype=int32_ref>
Tensor("mul_6:0", shape=(), dtype=int32)
a : 5
b : 3
c : 15
d : 45
댓글남기기