Skip to content

Commit fcabe08

Browse files
author
yu2nshuai
committed
tensor
1 parent 1a179f1 commit fcabe08

File tree

2 files changed

+246
-2
lines changed

2 files changed

+246
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
# TensorFlow2_Keras_API_Jupyter_demo
1+
# TensorFlow2_Keras_API_Jupyter_demo
2+
Tensorflow2 Keras 官方API文档demo,Jupyter版本学习笔记整理,在官方demo的基础上,丰富了一些样例。

tensor.ipynb

Lines changed: 244 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,255 @@
152152
"np.array(rank_2_tensor)"
153153
]
154154
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": 9,
158+
"metadata": {},
159+
"outputs": [
160+
{
161+
"data": {
162+
"text/plain": [
163+
"array([[1., 2.],\n",
164+
" [3., 4.],\n",
165+
" [5., 6.]], dtype=float16)"
166+
]
167+
},
168+
"execution_count": 9,
169+
"metadata": {},
170+
"output_type": "execute_result"
171+
}
172+
],
173+
"source": [
174+
"rank_2_tensor.numpy()"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": 27,
180+
"metadata": {},
181+
"outputs": [
182+
{
183+
"name": "stdout",
184+
"output_type": "stream",
185+
"text": [
186+
"tf.Tensor(\n",
187+
"[[ 6 8]\n",
188+
" [10 12]], shape=(2, 2), dtype=int32) \n",
189+
"\n",
190+
"tf.Tensor(\n",
191+
"[[-4 -4]\n",
192+
" [-4 -4]], shape=(2, 2), dtype=int32) \n",
193+
"\n",
194+
"tf.Tensor(\n",
195+
"[[ 5 12]\n",
196+
" [21 32]], shape=(2, 2), dtype=int32) \n",
197+
"\n",
198+
"tf.Tensor(\n",
199+
"[[0.2 0.33333333]\n",
200+
" [0.42857143 0.5 ]], shape=(2, 2), dtype=float64) \n",
201+
"\n",
202+
"tf.Tensor(\n",
203+
"[[19 22]\n",
204+
" [43 50]], shape=(2, 2), dtype=int32) \n",
205+
"\n"
206+
]
207+
}
208+
],
209+
"source": [
210+
"#张量通常包含浮点型和整型数据,但是还有许多其他数据类型,包括:\n",
211+
"#复杂的数值\n",
212+
"#字符串\n",
213+
"#您可以对张量执行基本数学运算,包括加法、逐元素乘法和矩阵乘法。\n",
214+
"\n",
215+
"a = tf.constant([\n",
216+
" [1,2],\n",
217+
" [3,4]\n",
218+
"],dtype=tf.int32)\n",
219+
"\n",
220+
"b = tf.constant([\n",
221+
" [5,6],\n",
222+
" [7,8]\n",
223+
"],dtype=tf.int32)\n",
224+
"\n",
225+
"\n",
226+
"print(tf.add(a,b),\"\\n\")\n",
227+
"print(tf.subtract(a,b),\"\\n\")\n",
228+
"print(tf.multiply(a,b),\"\\n\")\n",
229+
"print(tf.divide(a,b),\"\\n\")\n",
230+
"print(tf.matmul(a,b),\"\\n\")\n"
231+
]
232+
},
233+
{
234+
"cell_type": "code",
235+
"execution_count": 45,
236+
"metadata": {},
237+
"outputs": [
238+
{
239+
"name": "stdout",
240+
"output_type": "stream",
241+
"text": [
242+
"tf.Tensor(10.0, shape=(), dtype=float32) \n",
243+
"\n",
244+
"tf.Tensor([ 5. 10.], shape=(2,), dtype=float32) \n",
245+
"\n",
246+
"tf.Tensor([ 6. 7. 8. 9. 10.], shape=(5,), dtype=float32) \n",
247+
"\n",
248+
"tf.Tensor([1 1 1 1 1], shape=(5,), dtype=int64) \n",
249+
"\n",
250+
"tf.Tensor([4 4], shape=(2,), dtype=int64) \n",
251+
"\n",
252+
"tf.Tensor(\n",
253+
"[[0.01165623 0.03168492 0.08612853 0.23412165 0.63640857]\n",
254+
" [0.01165623 0.03168492 0.08612853 0.23412165 0.63640857]], shape=(2, 5), dtype=float32)\n"
255+
]
256+
}
257+
],
258+
"source": [
259+
"#各种运算都可以使用张量。\n",
260+
"#tf.reduce_max 是 TensorFlow 中的一个函数,用于计算张量(tensor)中的最大值。它可以沿着张量的一个或多个维度进行操作。\n",
261+
"a = tf.constant([\n",
262+
" [1,2,3,4,5],\n",
263+
" [6,7,8,9,10]\n",
264+
"],dtype=tf.float32)\n",
265+
"\n",
266+
"# Find the largest value\n",
267+
"print(tf.reduce_max(a),\"\\n\") #全局最大值\n",
268+
"print(tf.reduce_max(a,axis=1),\"\\n\") #1轴/行最大值\n",
269+
"print(tf.reduce_max(a,axis=0),\"\\n\") #0轴/列最大值\n",
270+
"\n",
271+
"# Find the index of the largest value\n",
272+
"print(tf.math.argmax(a,0),\"\\n\") #每列最大值索引\n",
273+
"print(tf.math.argmax(a,1),\"\\n\") #行最大值索引\n",
274+
"\n",
275+
"# Compute the softmax\n",
276+
"#tf.nn.softmax 函数用于计算张量中每个元素的 softmax 激活值。\n",
277+
"#Softmax 函数将输入的张量转换为一个新的张量,其中每个元素的取值范围在 0 到 1 之间,并且所有元素之和为 1。这使得 softmax 激活函数特别适合用于多分类问题中的输出层,其中每个输出节点表示一个类别的概率。\n",
278+
"print(tf.nn.softmax(a))"
279+
]
280+
},
281+
{
282+
"cell_type": "code",
283+
"execution_count": 48,
284+
"metadata": {},
285+
"outputs": [
286+
{
287+
"data": {
288+
"text/plain": [
289+
"<tf.Tensor: shape=(3, 2, 4, 5), dtype=float32, numpy=\n",
290+
"array([[[[0., 0., 0., 0., 0.],\n",
291+
" [0., 0., 0., 0., 0.],\n",
292+
" [0., 0., 0., 0., 0.],\n",
293+
" [0., 0., 0., 0., 0.]],\n",
294+
"\n",
295+
" [[0., 0., 0., 0., 0.],\n",
296+
" [0., 0., 0., 0., 0.],\n",
297+
" [0., 0., 0., 0., 0.],\n",
298+
" [0., 0., 0., 0., 0.]]],\n",
299+
"\n",
300+
"\n",
301+
" [[[0., 0., 0., 0., 0.],\n",
302+
" [0., 0., 0., 0., 0.],\n",
303+
" [0., 0., 0., 0., 0.],\n",
304+
" [0., 0., 0., 0., 0.]],\n",
305+
"\n",
306+
" [[0., 0., 0., 0., 0.],\n",
307+
" [0., 0., 0., 0., 0.],\n",
308+
" [0., 0., 0., 0., 0.],\n",
309+
" [0., 0., 0., 0., 0.]]],\n",
310+
"\n",
311+
"\n",
312+
" [[[0., 0., 0., 0., 0.],\n",
313+
" [0., 0., 0., 0., 0.],\n",
314+
" [0., 0., 0., 0., 0.],\n",
315+
" [0., 0., 0., 0., 0.]],\n",
316+
"\n",
317+
" [[0., 0., 0., 0., 0.],\n",
318+
" [0., 0., 0., 0., 0.],\n",
319+
" [0., 0., 0., 0., 0.],\n",
320+
" [0., 0., 0., 0., 0.]]]], dtype=float32)>"
321+
]
322+
},
323+
"execution_count": 48,
324+
"metadata": {},
325+
"output_type": "execute_result"
326+
}
327+
],
328+
"source": [
329+
"##张量有形状。下面是几个相关术语:\n",
330+
"\n",
331+
"#形状:张量的每个轴的长度(元素数量)。\n",
332+
"#秩:张量轴数。标量的秩为 0,向量的秩为 1,矩阵的秩为 2。\n",
333+
"#轴或维度:张量的一个特殊维度。\n",
334+
"#大小:张量的总项数,即形状矢量元素的乘积\n",
335+
"#注:虽然您可能会看到“二维张量”之类的表述,但 2 秩张量通常并不是用来描述二维空间。\n",
336+
"\n",
337+
"rank_4_tensor = tf.zeros([3,2,4,5])\n",
338+
"rank_4_tensor"
339+
]
340+
},
341+
{
342+
"cell_type": "code",
343+
"execution_count": 49,
344+
"metadata": {},
345+
"outputs": [
346+
{
347+
"name": "stdout",
348+
"output_type": "stream",
349+
"text": [
350+
"Type of every element: <dtype: 'float32'>\n",
351+
"Number of axes: 4\n",
352+
"Shape of tensor: (3, 2, 4, 5)\n",
353+
"Elements along axis 0 of tensor: 3\n",
354+
"Elements along the last axis of tensor: 5\n",
355+
"Total number of elements (3*2*4*5): 120\n"
356+
]
357+
}
358+
],
359+
"source": [
360+
"print(\"Type of every element:\", rank_4_tensor.dtype)\n",
361+
"print(\"Number of axes:\", rank_4_tensor.ndim)\n",
362+
"print(\"Shape of tensor:\", rank_4_tensor.shape)\n",
363+
"print(\"Elements along axis 0 of tensor:\", rank_4_tensor.shape[0])\n",
364+
"print(\"Elements along the last axis of tensor:\", rank_4_tensor.shape[-1])\n",
365+
"print(\"Total number of elements (3*2*4*5): \", tf.size(rank_4_tensor).numpy())"
366+
]
367+
},
368+
{
369+
"cell_type": "code",
370+
"execution_count": 52,
371+
"metadata": {},
372+
"outputs": [
373+
{
374+
"name": "stdout",
375+
"output_type": "stream",
376+
"text": [
377+
"tf.Tensor(4, shape=(), dtype=int32) \n",
378+
"\n",
379+
"tf.Tensor([3 2 4 5], shape=(4,), dtype=int32) \n",
380+
"\n"
381+
]
382+
}
383+
],
384+
"source": [
385+
"#但请注意,Tensor.ndim 和 Tensor.shape 特性不返回 Tensor 对象。如果您需要 Tensor,请使用 tf.rank 或 tf.shape 函数。这种差异不易察觉,但在构建计算图时(稍后)可能非常重要。\n",
386+
"print(tf.rank(rank_4_tensor),'\\n')\n",
387+
"print(tf.shape(rank_4_tensor),'\\n')"
388+
]
389+
},
155390
{
156391
"cell_type": "code",
157392
"execution_count": null,
158393
"metadata": {},
159394
"outputs": [],
160-
"source": []
395+
"source": [
396+
"#虽然通常用索引来指代轴,但是您始终要记住每个轴的含义。轴一般按照从全局到局部的顺序进行排序:首先是批次轴,随后是空间维度,最后是每个位置的特征。这样,在内存中,特征向量就会位于连续的区域。\n",
397+
"# https://tensorflow.google.cn/guide/tensor?hl=zh-cn 结合2个图理解\n",
398+
"# 3 2 4 5\n",
399+
"# 3 - Batch\n",
400+
"# 2 - Height\n",
401+
"# 4 - Width\n",
402+
"# 5 - Features"
403+
]
161404
}
162405
],
163406
"metadata": {

0 commit comments

Comments
 (0)