tensorflow 2.0 基础操作 之 不同 dim 对应的具体含义

Scalar (dim=0)

  • []
    • 0, 1, 3.2, …
  • l o s s = m s e ( o u t , y ) loss = mse(out, y) loss=mse(out,y)
  • a c c u r a c y accuracy accuracy


在这里插入图片描述

out = tf.random.uniform([4,10])

y = tf.range(4)
y = tf.one_hot(y, depth=10)

loss = tf.keras.losses.mse(y, out)
loss = tf.reduce_mean(loss)
loss   # tf.Tensor(0.2689442, shape=(), dtype=float32)

Vector (dim=1)

  • Bias
    • [out_put]

      在这里插入图片描述
net = layers.Dense(10)
net.build([4, 8])
net.kernel.shape  # matrix
# (8, 10)
net.bias   # vector
# <tf.Variable 'bias:0' shape=(10,) dtype=float32, numpy=array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>

Matrix (dim=2)

  • input x:[b, vec_dim]
  • weight: [input_dim, output_dim]
    在这里插入图片描述
x = tf.random.normal([4, 784])
net = layers.Dense(10)   # 784 --> 10
net.build([4, 784])
net(x).shape
# (4, 10)

Tensor (dim=3)

  • x: [b, seq_len, word_dim] [句子数目, 句子单词数目, 单词编码长度]
    • [b, 5, 5]

      在这里插入图片描述
(X_train, y_train), (X_test, y_test) = keras.datasets.imdb.load_data(num_words=10000)
x_train = keras.preprocessing.sequence.pad_sequences(X_train, maxlen=80)
x_train.shape   # (25000, 80)

emb = embedding(x_train)
emb.shape   # (25000, 80, 100)

out = rnn(emb[:4])
out.shape   # (4, 256)

Tensor (dim=4)

  • images: [b, h, w, 3]
  • feature maps: [b, h, w, c]


在这里插入图片描述

x = tf.random.normal([4,28,28,1])
net = layers.Conv2D(16, kernel_size=3)
net(x).shape   # (4, 26, 26, 16)

Tensor (dim=5)

  • Single task: [b, h, w, 3]
  • meta-learning:
    • [task_b, b, h, w, 3]

在这里插入图片描述

TensorFlow 2.0 中,`tensorflow.contrib` 已经被移除了,所以无法使用 `from tensorflow.contrib import learn` 这种方式导入 `learn` 模块。相应地,TensorFlow 2.0 中有一些新的 API 替代了 `learn` 模块中的功能。以下是一些替代方案: 1. 如果您需要使用 `learn.datasets` 模块中的数据集,请使用 `tensorflow_datasets` 库。例如,如果您需要使用 MNIST 数据集,您可以使用以下代码: ```python import tensorflow_datasets as tfds mnist, info = tfds.load('mnist', with_info=True) ``` 2. 如果您需要使用 `learn.preprocessing` 模块中的预处理功能,请使用 TensorFlow 2.0 中的 `tf.keras.preprocessing` 模块。例如,如果您需要使用 `learn.preprocessing.VocabularyProcessor` 类,请使用以下代码: ```python from tensorflow.keras.preprocessing.text import Tokenizer tokenizer = Tokenizer(num_words=1000) ``` 3. 如果您需要使用 `learn.models` 模块中的模型,请使用 TensorFlow 2.0 中的 `tf.keras` 模块。例如,如果您需要使用 `learn.models.LogisticRegression` 类,请使用以下代码: ```python from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential() model.add(Dense(1, input_dim=10, activation='sigmoid')) ``` 以上是一些常见的替代方案,但并不是全部。具体取决于您所使用的 `learn` 模块中的功能。您可以查看 TensorFlow 2.0 的官方文档,以了解有关替代方案的更多信息。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TransientYear

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值