Ben*_*y K 10 tensorflow tensorflow2.0
我有一个xx形状为的张量:
>>> xx.shape
TensorShape([32, 32, 256])
Run Code Online (Sandbox Code Playgroud)
如何添加前导None尺寸以获得:
>>> xx.shape
TensorShape([None, 32, 32, 256])
Run Code Online (Sandbox Code Playgroud)
我在这里看到了很多答案,但都与 TF 1.x 有关
TF 2.0 的直接方式是什么?
小智 2
您可以使用“None”或 numpy 的“newaxis”来创建新维度。
一般提示:您还可以使用 None 代替 np.newaxis;这些实际上是相同的对象。
下面是解释这两个选项的代码。
try:
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
print(tf.__version__)
# TensorFlow and tf.keras
from tensorflow import keras
# Helper libraries
import numpy as np
#### Import the Fashion MNIST dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
#Original Dimension
print(train_images.shape)
train_images1 = train_images[None,:,:,:]
#Add Dimension using None
print(train_images1.shape)
train_images2 = train_images[np.newaxis is None,:,:,:]
#Add dimension using np.newaxis
print(train_images2.shape)
#np.newaxis and none are same
np.newaxis is None
Run Code Online (Sandbox Code Playgroud)
上述代码的输出是
2.1.0
(60000, 28, 28)
(1, 60000, 28, 28)
(1, 60000, 28, 28)
True
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10185 次 |
| 最近记录: |