텐서플로우 1.x 버전 코드 실행하는 방법 알려주세요

조회수 984회

아래와 같은 코드를 받았는데 텐서플로우 1.0 버전의 코드라서 현재 2.4.1 버전으로 실행하니 계속 오류가 납니다.

session과 placeholder를 지원하지 않는 것은 알겠으나 어떻게 변경해야 할지 감이 잡히지 않습니다....

해당 부분 관련하여 조언부탁드립니다

#Tensorflow에서 값을 처리하기 위해 필요한 Session 유형의 변수를 sess로 생성
sess = tf.Session()

#9개의 픽셀 데이터에 대한 Tensor
#shape=(None, 7) : 입력하는 개수가 많을때는 정확히 알 수 없음(None), 한 번에 입력할 때 들어가는 데이터의 특징의 수 : 픽셀 9칸 -> 9
#Input Layer 노드의 수 = 7
X = tf.placeholder(tf.float32, shape=([None,7]),name="X")

#X에 입력한 픽셀의 실제 Label 값에 대한 Tensor
#shape=(None, 1) : Output Layer의 노드의 수 = 1
Y = tf.placeholder(tf.int32, shape=([None,1]),name="Y")

#Output Layer 노드의 수가 1개로 0인지 1인지 구별하기 위해 사용하는 방법이 One-hot encoding
# 250만이상 관객 수 동원에 대한 One-hot encoding
Y_one_hot = tf.one_hot(Y, 2, name="Y_one_hot")  

#[-1, 2]의 형태로 변환 -> -1 : 몇 개의 데이터를 사용할 지 모른다는 의미, 2 : 0, 1로 구별하겠다는 의미 
Y_one_hot = tf.reshape(Y_one_hot, [-1, 2])

#Hidden1_Layer
#Input Layer의 각 노드에 대한 가중치(Weight)
#W1 : 첫번째 Hidden Layer의 노드 3개
W1 = tf.Variable(tf.truncated_normal([7,3]),name='W1')

#Input Layer의 각 노드에 대한 편향(bias)
#b1 : 첫번째 Hidden Layer의 각 노드의 bias
b1 = tf.Variable(tf.truncated_normal([3]),name='b1')

#Hidden1_Layer : 입력한 데이터와 가중치의 곱셈 결과 + 편향(bias)
H1_logits = tf.matmul(X, W1) + b1

#Hidden2_Layer :  Hidden1_Layer결과값과 가중치의 곱셈 결과 + 편향(bias)
W2 = tf.Variable(tf.truncated_normal([3,2]),name="W2")

b2 = tf.Variable(tf.truncated_normal([2]), name='b2')

#Hidden2_Layer 계산 ㄱ밧
logits = tf.matmul(H1_logits, W2) + b2

#입력데이터와 출력 데이터의 관계(Relationship) 또는 패턴(Pattern)을 나타내기 위한 함수 : hypothesis
hypothesis = tf.nn.softmax(logits)

#Logits를 통해 그려진 그래프와, hypothesis를 통해 판별한 결과의 오차를 계산
cost_i = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,labels=Y_one_hot)

#전체 오차의 평균
cost = tf.reduce_mean(cost_i) 

#경사하강법(Gradient-Descent)를 이용하여 학습
#학습률(Learning_rate) : 0.05 -> 학습을 하는 과정에서 경사를 하강하는 폭의 정도 -> 작을 수록 폭이 좁음, 넓을 수록 폭이 넓음
optimization = tf.train.GradientDescentOptimizer(learning_rate=0.05).minimize(cost)

#우리가 선정한 hypothesis 함수를 기반으로 Classification한 결과
prediction = tf.argmax(hypothesis, 1) 

#Prediction의 결과가 실제 Label 값과 맞는지 여부
correct_prediction = tf.equal(prediction, tf.argmax(Y_one_hot, 1)) 

#Prediction의 정확성을 저장하는 변수
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

#Tensorflow의 변수 초기화
sess.run(tf.global_variables_initializer()) 

1 답변

  • pip install "tensorflow<2"


    ERROR: Could not find a version that satisfies the requirement tensorflow<2, ERROR: No matching distribution found for tensorflow<2 라는 에러가 나요 - dongahaeum

    이 에러는 tensorflow 1.x 가 사용하고 있는 파이썬 버전에 대해 준비되어 있지 않았기 때문에 발생하는 것입니다. tensorflow 1.x 가 2.x 로 넘어온 지 꽤 시간이 되었기 때문에, 최신버전인 python 3.9, 3.8 등을 사용하고 있다면 나올 수 있는 메시지 입니다.

    https://pypi.org/project/tensorflow/1.15.0/#files 여기를 보면, 어떤 버전의 tensorflow 에 어떤 python 버전이 지원되는지 확인할 수 있습니다.

    파이썬 3.7 64비트를 깔아보시기 바랍니다. (기존 파이썬은 삭제하시길 권장합니다.)

    • ERROR: Could not find a version that satisfies the requirement tensorflow<2, ERROR: No matching distribution found for tensorflow<2 라는 에러가 나요 알 수 없는 사용자 2021.3.3 16:55
    • 감사합니다!! 알 수 없는 사용자 2021.3.4 11:06

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)