TensorFlow 学习笔记1-tf.stack的用法

本文深入解析了TensorFlow中tf.stack、tf数组取值、tf.expand_dims和tf.boolean_mask等关键函数的使用方法,通过具体实例展示了如何在不同维度上进行张量拼接、取值、维度扩展和布尔掩码操作。
部署运行你感兴趣的模型镜像

                              tf.stack的用法

1 tf.stack

     tf.stack用于拼接两个tf 张量,拼接可以在不同的维度上进行,拼接后的新张量维度加1

     例子1:拼接1维数组:

x1=tf.constant([1,2,3])# shape:(3)
x2=tf.constant([3,4,5])# shape: (3)
#在第0个轴上拼接
y1=tf.stack([x1,x2],0) #shape=(2*3)
print(sess.run(y1))
-------------------y1
  [[1 2 3]
  [3 4 5]]
-------------------
#在第1个轴上拼接
#此处的axis最大值为1
y2=tf.stack([x1,x2],axis=1) #shape=(3*2)
print(sess.run(y2)
-------------------y2
 [[1 3]
 [2 4]
 [3 5]]
-------------------

例子2:拼接高维数组

>>>x2=tf.constant([[[1,1],[2,2]],[[3,2],[4,4]]])#shape:(2,2,2)
 [[[1 1]
  [2 2]]

 [[3 2]
  [4 4]]]
>>>x3=tf.constant([[[5,5],[6,6]],[[7,7],[8,8]]])#shape:(2,2,2)
 [[[5 5]
  [6 6]]

 [[7 7]
  [8 8]]]
#注意下面代码stack函数的参数axis值的变化从0到3
>>> z1=tf.stack([x2,x3],axis=0) #shape:(2*2*2*2)
>>> print(sess.run(z1))
[[[[1 1]
   [2 2]]

  [[3 2]
   [4 4]]]


 [[[5 5]
   [6 6]]

  [[7 7]
   [8 8]]]]
>>> z2=tf.stack([x2,x3],1)
>>> print(sess.run(z2))
[[[[1 1]
   [2 2]]

  [[5 5]
   [6 6]]]


 [[[3 2]
   [4 4]]

  [[7 7]
   [8 8]]]]

>>> z3=tf.stack([x2,x3],2)
>>> print(sess.run(z3))
[[[[1 1]
   [5 5]]

  [[2 2]
   [6 6]]]


 [[[3 2]
   [7 7]]

  [[4 4]
   [8 8]]]]

>>> z4=tf.stack([x2,x3],axis=3) # axis的最大值为3 比x2的维度大1
>>> print(sess.run(z4))
[[[[1 5]
   [1 5]]

  [[2 6]
   [2 6]]]


 [[[3 7]
   [2 7]]

  [[4 8]
   [4 8]]]]

2  tf数组取值

 

>>>x2=tf.constant([[[1,1],[2,2]],[[3,2],[4,4]]])#shape:(2,2,2)
 [[[1 1]
  [2 2]]

 [[3 2]
  [4 4]]]

>>> y=x2[:,:,0] #取最后一维的索引0的值
>>> y.shape
TensorShape([Dimension(2), Dimension(2)])
#注意此处的y的shape大小是2*2 二维不是2*2*1三维

3 tf.expand_dims 

>>> x=tf.constant([[1,2],[3,4]])
>>> sess=tf.Session()
>>> print(sess.run(x))
  [[1 2]
   [3 4]]
>>> y=tf.expand_dims(x,-1) # 对x的最后的维度添加1维
>>> y.shape
TensorShape([Dimension(2), Dimension(2), Dimension(1)])
>>> print(sess.run(y))
[[[1]
  [2]]
 [[3]
  [4]]]

>>> y=tf.expand_dims(x,1)  # 在第1维度添加1维
>>> print(sess.run(y))
[[[1 2]]

 [[3 4]]]

4 tf.boolean_mask

>>> y=tf.constant([1,2,3,4,5,6,7,8])
>>> print(sess.run(tf.boolean_mask(y,[1])))
>>> mask=y >2
>>> print(sess.run(mask))
[False False  True  True  True  True  True  True]
>>> print(sess.run(tf.boolean_mask(y,mask)))
[3 4 5 6 7 8]

 

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

FAILURE: Build completed with 10 failures. 1: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:checkDebugAarMetadata'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. ============================================================================== 2: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:processDebugNavigationResources'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. ============================================================================== 3: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:mapDebugSourceSetPaths'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. ============================================================================== 4: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:mergeDebugResources'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. ============================================================================== 5: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:processDebugMainManifest'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. ============================================================================== 6: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:mergeDebugAssets'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. ============================================================================== 7: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:mergeDebugJavaResource'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. ============================================================================== 8: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:checkDebugDuplicateClasses'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. ============================================================================== 9: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:desugarDebugFileDependencies'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. ============================================================================== 10: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:mergeDebugNativeLibs'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.tensorflow:tensorflow-lite-support:4.0.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-support/4.0.0/tensorflow-lite-support-4.0.0.pom Required by: project :app > Could not find org.tensorflow:tensorflow-lite-metadata:2.17.0. Searched in the following locations: - https://dl.google.com/dl/android/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom - https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/2.17.0/tensorflow-lite-metadata-2.17.0.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org.再次尝试build时出现上述错误怎么解决
最新发布
10-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值