{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "5rmpybwysXGV" }, "source": [ "````{admonition} Copyright 2020 The TensorFlow Authors.\n", "```\n", "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "```\n", "````" ] }, { "cell_type": "markdown", "metadata": { "id": "hrXv0rU9sIma" }, "source": [ "# TensorFlow 基础知识" ] }, { "cell_type": "markdown", "metadata": { "id": "7S0BwJ_8sLu7" }, "source": [ "
![]() | \n",
" ![]() | \n",
" ![]() | \n",
" ![]() | \n",
"
tf.Tensor
对象表示的多维数组或张量进行运算。下面一个二维张量:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "6ZqX5RnbBS1f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[1. 2. 3.]\n",
" [4. 5. 6.]], shape=(2, 3), dtype=float32)\n",
"(2, 3)\n",
"tf.Variable
值和`tf.function`图。这使得模型可以不依赖原来的 Python 程序独立运行。\n",
"\n",
"下面是导出简单 `tf.Module` 对象的完整例子:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"id": "1MqEcZOqPBDV"
},
"outputs": [],
"source": [
"class MyModule(tf.Module):\n",
" def __init__(self, value):\n",
" self.weight = tf.Variable(value)\n",
"\n",
" @tf.function\n",
" def multiply(self, x):\n",
" return x * self.weight"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"id": "la2G82HfVfU0"
},
"outputs": [
{
"data": {
"text/plain": [
"