Skip to content

Instantly share code, notes, and snippets.

@gihyunkim
Created July 24, 2019 06:49
Show Gist options
  • Save gihyunkim/29178624f17b85f5559535d34da2c02d to your computer and use it in GitHub Desktop.
Save gihyunkim/29178624f17b85f5559535d34da2c02d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# # practice tensorflow\n",
"\n",
"### constant()\n",
"\n",
"- 3.0, 4.0의 정수 값을 가지는 노드와 operation을 만든다. : graph를 만든다.\n",
"- session을 만들어서 sess.run으로 실행 후 업데이트 또는 결과를 리턴"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sess.run(node1, node2): [3.0, 4.0]\n",
"sess.run(node3) : 7.0\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"# build graph\n",
"node1 = tf.constant(3.0, tf.float32)\n",
"node2 = tf.constant(4.0) # tf.float32가 암묵적으로 들어감\n",
"node3 = tf.add(node1, node2) # same as node3 = node1 + node2\n",
"\n",
"#sess.run() execute and return value or update\n",
"with tf.Session() as sess:\n",
" print(\"sess.run(node1, node2): \", sess.run([node1,node2]))\n",
" print(\"sess.run(node3) : \", sess.run(node3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### placeholder()\n",
"\n",
"- placeholder : placeholder란 노드를 만든다. * 값이 들어있지 않음\n",
"- feed_dict를 이용해서 값을 넣어줌.\n",
"- shape를 정해주지 않으면 default : None, None \n",
"- None : 아무 크기나 가능"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7.5\n",
"[3. 7.]\n"
]
}
],
"source": [
"a = tf.placeholder(tf.float32)\n",
"b = tf.placeholder(tf.float32)\n",
"adder_node = a+b\n",
"\n",
"with tf.Session() as sess:\n",
" print(sess.run(adder_node, feed_dict={a:3, b: 4.5}))\n",
" print(sess.run(adder_node, feed_dict={a:[1,3], b: [2,4]}))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tensor\n",
"- rank : 몇 차원으로 되어있냐\n",
"- shape : 각 element에 몇 개가 들어있냐\n",
"- datatype: 대부분의 경우 flaot32 or int32"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment