Skip to content
Permalink
Browse files

tensorflow2.0

  • Loading branch information...
czy36mengfei committed Mar 11, 2019
1 parent f485af1 commit 4a4b6055c9462bb37a89b7f8b4715b3baea3a758
Showing with 165 additions and 0 deletions.
  1. +165 −0 007-Variables.ipynb
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TensorFlow教程-Variables"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"创建一个变量"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32, numpy=\n",
"array([[1., 1., 1.],\n",
" [1., 1., 1.]], dtype=float32)>\n",
"no gpu\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"my_var = tf.Variable(tf.ones([2,3]))\n",
"print(my_var)\n",
"try:\n",
" with tf.device(\"/device:GPU:0\"):\n",
" v = tf.Variable(tf.zeros([10, 10]))\n",
" print(v)\n",
"except:\n",
" print('no gpu')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"使用变量\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(9.0, shape=(), dtype=float32)\n"
]
}
],
"source": [
"a = tf.Variable(1.0)\n",
"b = (a+2) *3\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(9.0, shape=(), dtype=float32)\n"
]
}
],
"source": [
"a = tf.Variable(1.0)\n",
"b = (a.assign_add(2)) *3\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"变量跟踪\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=10.0>, <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=1.0>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=0>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=1>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=2>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=4>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=5>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=6>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=7>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=8>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=9>)\n"
]
},
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class MyModuleOne(tf.Module):\n",
" def __init__(self):\n",
" self.v0 = tf.Variable(1.0)\n",
" self.vs = [tf.Variable(x) for x in range(10)]\n",
" \n",
"class MyOtherModule(tf.Module):\n",
" def __init__(self):\n",
" self.m = MyModuleOne()\n",
" self.v = tf.Variable(10.0)\n",
" \n",
"m = MyOtherModule()\n",
"print(m.variables)\n",
"len(m.variables) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 4a4b605

Please sign in to comment.
You can’t perform that action at this time.