TensorFlow

TensorFlow 기초 3 - TF의 구조 (Graph로 설계된 내용은 Session에 실행)

코딩탕탕 2022. 11. 28. 15:42

 

print('---TF의 구조 (Graph로 설계된 내용은 Session에 실행)---')
g1 = tf.Graph()

with g1.as_default():  # 특정 자원 처리를 한 후 자동 close()
    c1 = tf.constant(1, name='c_one') # tensor 이름을 선언
    c1_1 = tf.constant(1, name='c_one1') # tensor 이름을 선언
    print(c1)
    print(type(c1))  # Tensor 객체
    
    print(c1.op) # tf.Operation 객체(내부적으로 (console 내용) 생성 됨)
    print('---')
    print(g1.as_graph_def())
    
print('--------------------')
g2 = tf.Graph()

with g2.as_default():
    v1 = tf.Variable(initial_value=1, name='v1')
    print(v1)
    print(type(v1)) # ResourceVariable 객체
    print(v1.op)

print('~~~')
print(g2.as_graph_def())
    
    
    
<console>
---TF의 구조 (Graph로 설계된 내용은 Session에 실행)---
Tensor("c_one:0", shape=(), dtype=int32)
<class 'tensorflow.python.framework.ops.Tensor'>
name: "c_one"
op: "Const"
attr {
  key: "dtype"
  value {
    type: DT_INT32
  }
}
attr {
  key: "value"
  value {
    tensor {
      dtype: DT_INT32
      tensor_shape {
      }
      int_val: 1
    }
  }
}

---
node {
  name: "c_one"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 1
      }
    }
  }
}
node {
  name: "c_one1"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 1
      }
    }
  }
}
versions {
  producer: 1286
}

--------------------
<tf.Variable 'v1:0' shape=() dtype=int32>
<class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
name: "v1"
op: "VarHandleOp"
attr {
  key: "_class"
  value {
    list {
      s: "loc:@v1"
    }
  }
}
attr {
  key: "allowed_devices"
  value {
    list {
    }
  }
}
attr {
  key: "container"
  value {
    s: ""
  }
}
attr {
  key: "dtype"
  value {
    type: DT_INT32
  }
}
attr {
  key: "shape"
  value {
    shape {
    }
  }
}
attr {
  key: "shared_name"
  value {
    s: "v1"
  }
}

~~~
node {
  name: "v1/Initializer/initial_value"
  op: "Const"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@v1"
      }
    }
  }
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 1
      }
    }
  }
}
node {
  name: "v1"
  op: "VarHandleOp"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@v1"
      }
    }
  }
  attr {
    key: "allowed_devices"
    value {
      list {
      }
    }
  }
  attr {
    key: "container"
    value {
      s: ""
    }
  }
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "shape"
    value {
      shape {
      }
    }
  }
  attr {
    key: "shared_name"
    value {
      s: "v1"
    }
  }
}
node {
  name: "v1/IsInitialized/VarIsInitializedOp"
  op: "VarIsInitializedOp"
  input: "v1"
}
node {
  name: "v1/Assign"
  op: "AssignVariableOp"
  input: "v1"
  input: "v1/Initializer/initial_value"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "validate_shape"
    value {
      b: false
    }
  }
}
node {
  name: "v1/Read/ReadVariableOp"
  op: "ReadVariableOp"
  input: "v1"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
}
versions {
  producer: 1286
}