편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2021.03.10

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


    아래와 같은 코드를 받았는데 텐서플로우 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()) 
    
  • 프로필 알 수 없는 사용자님의 편집
    날짜2021.03.03

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


    아래와 같은 코드를 받았는데 텐서플로우 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())