Skip to content

Commit

Permalink
fix: untitled sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Valdes committed Dec 23, 2024
1 parent 7485470 commit d3debf5
Showing 1 changed file with 295 additions and 1 deletion.
296 changes: 295 additions & 1 deletion Untitled.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,303 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "a3394f67-a3fc-4c26-887e-70c315703d65",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"4\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"\n",
"a = tf.constant([1, 2, 3, 4, 5])\n",
"first_three_elem = a[:3]\n",
"fourth_elem = a[3]\n",
"\n",
"# Directly convert to NumPy or Python objects with .numpy()\n",
"print(first_three_elem.numpy()) # [1 2 3]\n",
"print(fourth_elem.numpy()) \n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e3b108ce-d5ec-421f-a655-5b9c6ecc299d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor([1. 1.1 2.1 3.1], shape=(4,), dtype=float32)\n"
]
}
],
"source": [
"a = tf.constant([1.0, 1.1, 2.1, 3.1], dtype=tf.float32, name='a_const')\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d33d85e8-43e8-4a20-aa31-6303b774d0da",
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"\n",
"# Instead of using a placeholder + session.run, define everything as tensors:\n",
"a = tf.constant([[0.1, 0.2, 0.3]], dtype=tf.float32)\n",
"b = tf.constant([[10, 10, 10]], dtype=tf.float32)\n",
"\n",
"# Perform your operation directly\n",
"c = a + b"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "6ed0c75b-a964-48df-a9dd-b6e0a8dcbafa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor([[10.1 10.2 10.3]], shape=(1, 3), dtype=float32)\n"
]
}
],
"source": [
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "45bdd81b-c86d-4abc-804f-3d43a60382be",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[10.1 10.2 10.3]]\n",
"[[7. 7. 7.]\n",
" [7. 7. 7.]]\n"
]
}
],
"source": [
"import numpy as np\n",
"# In TF 2.x, we don't use placeholders or sessions; instead, we define tensors and operate on them directly.\n",
"\n",
"# We'll simulate your input shape (None, 3) by creating two tf.constant tensors from Python lists or NumPy arrays.\n",
"# 'None' in the first dimension simply means it can be any batch size.\n",
"\n",
"a_vals_1 = [[0.1,0.2,0.3]]\n",
"b_vals_1 = [[10,10,10]]\n",
"\n",
"# Create TF tensors\n",
"a_1 = tf.constant(a_vals_1, dtype=tf.float32)\n",
"b_1 = tf.constant(b_vals_1, dtype=tf.float32)\n",
"\n",
"# Add the tensors\n",
"c_1 = a_1 + b_1\n",
"print(c_1.numpy()) # [[10.1 10.2 10.3]]\n",
"\n",
"# For the multi-row example\n",
"v_a = np.array([[1,2,3],[4,5,6]], dtype=np.float32)\n",
"v_b = np.array([[6,5,4],[3,2,1]], dtype=np.float32)\n",
"\n",
"a_2 = tf.constant(v_a, dtype=tf.float32)\n",
"b_2 = tf.constant(v_b, dtype=tf.float32)\n",
"\n",
"c_2 = a_2 + b_2\n",
"print(c_2.numpy()) "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "0ac17c30-2312-40a2-9df9-66d088e1a6b5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-0.24501422 -0.5116961 -0.44979703]]\n",
"[[4 5]]\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"\n",
"# Variable initialized randomly\n",
"var = tf.Variable(\n",
" tf.random.normal([1, 3]),\n",
" name=\"first_variable\",\n",
" dtype=tf.float32\n",
")\n",
"\n",
"# Variable initialized with constant values\n",
"init_val = np.array([4, 5])\n",
"var2 = tf.Variable(\n",
" init_val.reshape([1, 2]),\n",
" name=\"second_variable\",\n",
" dtype=tf.int32\n",
")\n",
"\n",
"# In TF 2.x, no session or global init call is needed.\n",
"print(var.numpy())\n",
"print(var2.numpy())"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "b3c566e4-6643-4af4-a13f-ed69c6ac259f",
"metadata": {},
"outputs": [],
"source": [
"var2 = tf.Variable(\n",
" tf.zeros([1, 2], dtype=tf.int32),\n",
" name=\"variable\",\n",
" trainable=False\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "28ad88bb-f378-47b0-aaf9-87c43b1260f1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-1.6479137\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"\n",
"# Equivalent to tf.constant(3.0, name='constant1')\n",
"const1 = tf.constant(3.0, name='constant1')\n",
"\n",
"# Equivalent to tf.get_variable(\"variable1\", shape=[1,2], dtype=tf.float32)\n",
"var = tf.Variable(\n",
" tf.random.normal([1,2]),\n",
" name=\"variable1\",\n",
" dtype=tf.float32\n",
")\n",
"\n",
"# Equivalent to tf.get_variable(\"variable2\", shape=[1,2], trainable=False, dtype=tf.float32)\n",
"var2 = tf.Variable(\n",
" tf.random.normal([1,2]),\n",
" name=\"variable2\",\n",
" dtype=tf.float32,\n",
" trainable=False\n",
")\n",
"\n",
"# These ops are directly computed in eager mode:\n",
"op1 = const1 * var\n",
"op2 = op1 + var2\n",
"op3 = tf.reduce_mean(op2)\n",
"\n",
"# Print results immediately (no Session needed)\n",
"print(op3.numpy())"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "0841827f-1770-4b6c-97e7-01c1aedde23b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:Trace already enabled\n"
]
}
],
"source": [
"logdir = \"logs/graph_demo\"\n",
"writer = tf.summary.create_file_writer(logdir)\n",
"\n",
"# Actually record the trace\n",
"with writer.as_default():\n",
" tf.summary.trace_on(graph=True, profiler=False)\n",
" tf.summary.trace_export(name=\"my_graph\", step=0)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "bb359e92-3724-4e0b-a247-1138000553c6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Reusing TensorBoard on port 6006 (pid 36911), started 0:00:18 ago. (Use '!kill 36911' to kill it.)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" <iframe id=\"tensorboard-frame-794b75c9ae2164bd\" width=\"100%\" height=\"800\" frameborder=\"0\">\n",
" </iframe>\n",
" <script>\n",
" (function() {\n",
" const frame = document.getElementById(\"tensorboard-frame-794b75c9ae2164bd\");\n",
" const url = new URL(\"/\", window.location);\n",
" const port = 6006;\n",
" if (port) {\n",
" url.port = port;\n",
" }\n",
" frame.src = url;\n",
" })();\n",
" </script>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%tensorboard --logdir logs/graph_demo\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c253276-6b56-4cff-a81b-c863939a035c",
"metadata": {},
"outputs": [],
"source": []
}
Expand Down

0 comments on commit d3debf5

Please sign in to comment.