Skip to content

Commit

Permalink
op
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYang1994 authored and YunYang1994 committed Mar 8, 2019
1 parent d4cf31d commit d934f36
Show file tree
Hide file tree
Showing 3 changed files with 425 additions and 0 deletions.
171 changes: 171 additions & 0 deletions 1-Introduction/.ipynb_checkpoints/basic_operations-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[1. 1. 1.]\n",
" [1. 1. 1.]], shape=(2, 3), dtype=float32)\n",
"tf.Tensor(\n",
"[[10. 1. 1.]\n",
" [ 1. 1. 1.]], shape=(2, 3), dtype=float32)\n"
]
}
],
"source": [
"###======================================= assign value ===================================#\n",
"\n",
"a = tf.ones([2,3])\n",
"print(a)\n",
"\n",
"# a[0,0] = 10 => TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment\n",
"\n",
"a = tf.Variable(a)\n",
"a[0,0].assign(10)\n",
"b = a.read_value()\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a + b : 5\n",
"Addition with constants: tf.Tensor(5, shape=(), dtype=int32)\n",
"Addition with constants: tf.Tensor(5, shape=(), dtype=int32)\n",
"a * b : 6\n",
"Multiplication with constants: tf.Tensor(6, shape=(), dtype=int32)\n",
"Multiplication with constants: tf.Tensor(6, shape=(), dtype=int32)\n",
"Multiplication with matrixes: tf.Tensor([[12.]], shape=(1, 1), dtype=float32)\n",
"broadcast matrix in Multiplication: tf.Tensor(\n",
"[[6. 6.]\n",
" [6. 6.]], shape=(2, 2), dtype=float32)\n"
]
}
],
"source": [
"###======================================= add, multiply, div. etc ===================================#\n",
"\n",
"a = tf.constant(2)\n",
"b = tf.constant(3)\n",
"\n",
"print(\"a + b :\" , a.numpy() + b.numpy())\n",
"print(\"Addition with constants: \", a+b)\n",
"print(\"Addition with constants: \", tf.add(a, b))\n",
"print(\"a * b :\" , a.numpy() * b.numpy())\n",
"print(\"Multiplication with constants: \", a*b)\n",
"print(\"Multiplication with constants: \", tf.multiply(a, b))\n",
"\n",
"\n",
"# ----------------\n",
"# More in details:\n",
"# Matrix Multiplication from TensorFlow official tutorial\n",
"\n",
"# Create a Constant op that produces a 1x2 matrix. The op is\n",
"# added as a node to the default graph.\n",
"#\n",
"# The value returned by the constructor represents the output\n",
"# of the Constant op.\n",
"matrix1 = tf.constant([[3., 3.]])\n",
"\n",
"# Create another Constant that produces a 2x1 matrix.\n",
"matrix2 = tf.constant([[2.],[2.]])\n",
"\n",
"# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.\n",
"# The returned value, 'product', represents the result of the matrix\n",
"# multiplication.\n",
"product = tf.matmul(matrix1, matrix2)\n",
"print(\"Multiplication with matrixes:\", product)\n",
"\n",
"# broadcast matrix in Multiplication\n",
"\n",
"print(\"broadcast matrix in Multiplication:\", matrix1 * matrix2)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(2.0, shape=(), dtype=float32) tf.Tensor(2, shape=(), dtype=int32)\n"
]
}
],
"source": [
"###===================================== cast operations =====================================#\n",
"\n",
"a = tf.convert_to_tensor(2.)\n",
"b = tf.cast(a, tf.int32)\n",
"print(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 3\n",
"tf.Tensor(2, shape=(), dtype=int32) tf.Tensor(3, shape=(), dtype=int32)\n"
]
}
],
"source": [
"###===================================== shape operations ===================================#\n",
"\n",
"a = tf.ones([2,3])\n",
"print(a.shape[0], a.shape[1]) # 2, 3\n",
"shape = tf.shape(a) # a tensor\n",
"print(shape[0], shape[1])"
]
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
171 changes: 171 additions & 0 deletions 1-Introduction/basic_operations.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[1. 1. 1.]\n",
" [1. 1. 1.]], shape=(2, 3), dtype=float32)\n",
"tf.Tensor(\n",
"[[10. 1. 1.]\n",
" [ 1. 1. 1.]], shape=(2, 3), dtype=float32)\n"
]
}
],
"source": [
"###======================================= assign value ===================================#\n",
"\n",
"a = tf.ones([2,3])\n",
"print(a)\n",
"\n",
"# a[0,0] = 10 => TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment\n",
"\n",
"a = tf.Variable(a)\n",
"a[0,0].assign(10)\n",
"b = a.read_value()\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a + b : 5\n",
"Addition with constants: tf.Tensor(5, shape=(), dtype=int32)\n",
"Addition with constants: tf.Tensor(5, shape=(), dtype=int32)\n",
"a * b : 6\n",
"Multiplication with constants: tf.Tensor(6, shape=(), dtype=int32)\n",
"Multiplication with constants: tf.Tensor(6, shape=(), dtype=int32)\n",
"Multiplication with matrixes: tf.Tensor([[12.]], shape=(1, 1), dtype=float32)\n",
"broadcast matrix in Multiplication: tf.Tensor(\n",
"[[6. 6.]\n",
" [6. 6.]], shape=(2, 2), dtype=float32)\n"
]
}
],
"source": [
"###======================================= add, multiply, div. etc ===================================#\n",
"\n",
"a = tf.constant(2)\n",
"b = tf.constant(3)\n",
"\n",
"print(\"a + b :\" , a.numpy() + b.numpy())\n",
"print(\"Addition with constants: \", a+b)\n",
"print(\"Addition with constants: \", tf.add(a, b))\n",
"print(\"a * b :\" , a.numpy() * b.numpy())\n",
"print(\"Multiplication with constants: \", a*b)\n",
"print(\"Multiplication with constants: \", tf.multiply(a, b))\n",
"\n",
"\n",
"# ----------------\n",
"# More in details:\n",
"# Matrix Multiplication from TensorFlow official tutorial\n",
"\n",
"# Create a Constant op that produces a 1x2 matrix. The op is\n",
"# added as a node to the default graph.\n",
"#\n",
"# The value returned by the constructor represents the output\n",
"# of the Constant op.\n",
"matrix1 = tf.constant([[3., 3.]])\n",
"\n",
"# Create another Constant that produces a 2x1 matrix.\n",
"matrix2 = tf.constant([[2.],[2.]])\n",
"\n",
"# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.\n",
"# The returned value, 'product', represents the result of the matrix\n",
"# multiplication.\n",
"product = tf.matmul(matrix1, matrix2)\n",
"print(\"Multiplication with matrixes:\", product)\n",
"\n",
"# broadcast matrix in Multiplication\n",
"\n",
"print(\"broadcast matrix in Multiplication:\", matrix1 * matrix2)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(2.0, shape=(), dtype=float32) tf.Tensor(2, shape=(), dtype=int32)\n"
]
}
],
"source": [
"###===================================== cast operations =====================================#\n",
"\n",
"a = tf.convert_to_tensor(2.)\n",
"b = tf.cast(a, tf.int32)\n",
"print(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 3\n",
"tf.Tensor(2, shape=(), dtype=int32) tf.Tensor(3, shape=(), dtype=int32)\n"
]
}
],
"source": [
"###===================================== shape operations ===================================#\n",
"\n",
"a = tf.ones([2,3])\n",
"print(a.shape[0], a.shape[1]) # 2, 3\n",
"shape = tf.shape(a) # a tensor\n",
"print(shape[0], shape[1])"
]
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit d934f36

Please sign in to comment.