tensorflow的keras实现搭配dataset 之一

tensorflow的keras实现搭配dataset,几种形式都工作!

tensorflow,keras Sequential模式下:

见代码:

from tensorflow import keras as ks
import tensorflow as tf

# Generate dummy data
import numpy as np
x_train = np.random.random((1000, 20))
y_train = ks.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = ks.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)


batch_size = 100
steps_per_epoch = int(np.ceil(x_train.shape[0]/batch_size))

train_ds = tf.data.Dataset.from_tensor_slices((x_train,y_train))
train_ds = train_ds.batch(batch_size)   # batch 能给数据集增加批维度
train_ds = train_ds.repeat()

train_it = train_ds.make_one_shot_iterator()
x_train_it, y_train_it = train_it.get_next()


test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test))
test_ds = test_ds.batch(batch_size)
test_ds = test_ds.repeat()

model = ks.models.Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(ks.layers.Dense(64, activation='relu', input_dim=20))
model.add(ks.layers.Dropout(0.5))
model.add(ks.layers.Dense(64, activation='relu'))
model.add(ks.layers.Dropout(0.5))
model.add(ks.layers.Dense(10, activation='softmax'))

sgd = ks.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd,   metrics=['accuracy'])

# passing the data to the model with the below to style, both work
model.fit(x_train_it, y_train_it, epochs=20, steps_per_epoch=steps_per_epoch)
print("(+("*20,'\n'*4)
model.fit(train_ds, epochs=20, steps_per_epoch=steps_per_epoch)

score = model.evaluate(test_ds, steps=128)
print(score)

 

转载于:https://www.cnblogs.com/wdmx/p/10256713.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以用 pip 安装 TensorFlowKeras:pip install tensorflow 和 pip install keras。如果你想要安装特定的 TensorFlow 版本,你可以使用 pip install tensorflow==<version>。 ### 回答2: tensorflow keras 是一种机器学习框架,在安装之前,我们首先需要安装好 Python 环境。接下来,我们可以通过以下步骤来安装 tensorflow keras: 1. 打开终端或命令行界面,并确保已经安装了 pip,pip 是 Python 包管理器,用于安装第三方软件包。 2. 在终端中输入以下命令来安装 tensorflow keras: `pip install tensorflow` 这会自动安装最新版本的 tensorflow 包。 3. 安装完成后,我们可以在 Python 中导入 tensorflow 包,并验证其安装是否成功: ```python import tensorflow as tf print(tf.__version__) ``` 这将输出 tensorflow 版本号,表示 tensorflow 安装成功。 4. 接下来,我们可以安装 keraskeras 是基于 tensorflow 的高级神经网络 API。在终端中输入以下命令安装 keras: `pip install keras` 这将自动安装最新版本的 keras。 5. 安装完成后,我们可以在 Python 中导入 keras,并验证其安装是否成功: ```python import keras print(keras.__version__) ``` 这将输出 keras 版本号,表示 keras 安装成功。 至此,我们已经成功安装了 tensorflow keras。现在,我们可以开始使用这个强大的机器学习框架来构建和训练深度学习模型了。 ### 回答3: 要安装TensorFlowKeras,你可以按照以下步骤进行操作: 1. 确保你的计算机已经安装好Python,并且Python版本在3.5以上。 2. 打开命令行界面,使用以下命令安装TensorFlow: ``` pip install tensorflow ``` 如果你的计算机上同时安装有GPU并且想要使用GPU加速,可以使用以下命令来安装TensorFlow GPU版本: ``` pip install tensorflow-gpu ``` 3. 安装完成后,使用以下命令来验证TensorFlow的安装情况: ``` python -c "import tensorflow as tf;print(tf.__version__)" ``` 如果成功安装,将会显示出TensorFlow的版本号。 4. 接下来,你可以安装Keras。使用以下命令来安装Keras: ``` pip install keras ``` 另外,Keras已经成为TensorFlow的一部分,如果你已经成功安装了TensorFlow,在导入TensorFlow时,也可以通过调用`tf.keras`来使用Keras。 至此,你已经成功安装了TensorFlowKeras。你可以通过导入相应库和模块来使用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值