当前位置:   article > 正文

DNN TensorFlow_dnn tensorflow只能处理浮点数吗

dnn tensorflow只能处理浮点数吗

一、相关教程

1.中文教程 https://tf.wiki/zh_hans/basic/models.html#model-layer

   内容丰富而全面,好评100分

   函数的功能介绍比较细致和全面

多层感知机分类

https://tf.wiki/zh_hans/basic/models.html#model-layer

  1. class MNISTLoader():
  2. def __init__(self):
  3. mnist = tf.keras.datasets.mnist
  4. (self.train_data, self.train_label), (self.test_data, self.test_label) = mnist.load_data()
  5. # MNIST中的图像默认为uint8(0-255的数字)。以下代码将其归一化到0-1之间的浮点数,并在最后增加一维作为颜色通道
  6. self.train_data = np.expand_dims(self.train_data.astype(np.float32) / 255.0, axis=-1) # [60000, 28, 28, 1]
  7. self.test_data = np.expand_dims(self.test_data.astype(np.float32) / 255.0, axis=-1) # [10000, 28, 28, 1]
  8. self.train_label = self.train_label.astype(np.int32) # [60000]
  9. self.test_label = self.test_label.astype(np.int32) # [10000]
  10. self.num_train_data, self.num_test_data = self.train_data.shape[0], self.test_data.shape[0]
  11. def get_batch(self, batch_size):
  12. # 从数据集中随机取出batch_size个元素并返回,随机划重点
  13. index = np.random.randint(0, self.num_train_data, batch_size)
  14. return self.train_data[index, :], self.train_label[index]
  1. class MLP(tf.keras.Model):
  2. def __init__(self):
  3. super().__init__()
  4. self.flatten = tf.keras.layers.Flatten() # Flatten层将除第一维(batch_size)以外的维度展平
  5. self.dense1 = tf.keras.layers.Dense(units=100, activation=tf.nn.relu)
  6. self.dense2 = tf.keras.layers.Dense(units=10)
  7. def call(self, inputs): # [batch_size, 28, 28, 1]
  8. x = self.flatten(inputs) # [batch_size, 784] 输入层
  9. x = self.dense1(x) # [batch_size, 100] 隐藏层
  10. x = self.dense2(x) # [batch_size, 10] 输出层
  11. output = tf.nn.softmax(x)
  12. return output
  1. num_epochs = 5#过5遍训练集
  2. batch_size = 50#一次迭代所使用的样本量,每次迭代更新一次网络结果的参数
  3. learning_rate = 0.001
  1. model = MLP()
  2. data_loader = MNISTLoader()
  3. optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)

 

  1. #5遍训练集,所需要的迭代次数
  2. num_batches = int(data_loader.num_train_data // batch_size * num_epochs)
  3. for batch_index in range(num_batches):
  4. X, y = data_loader.get_batch(batch_size)
  5. with tf.GradientTape() as tape:
  6. y_pred = model(X)
  7. loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y, y_pred=y_pred)
  8. loss = tf.reduce_mean(loss)
  9. print("batch %d: loss %f" % (batch_index, loss.numpy()))
  10. grads = tape.gradient(loss, model.variables)
  11. optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
  1. #交叉熵作为损失函数,具体公式可百度
  2. #loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y, y_pred=y_pred)
  1. sparse_categorical_accuracy = tf.keras.metrics.SparseCategoricalAccuracy()
  2. num_batches = int(data_loader.num_test_data // batch_size)
  3. for batch_index in range(num_batches):
  4. start_index, end_index = batch_index * batch_size, (batch_index + 1) * batch_size
  5. y_pred = model.predict(data_loader.test_data[start_index: end_index])
  6. sparse_categorical_accuracy.update_state(y_true=data_loader.test_label[start_index: end_index], y_pred=y_pred)
  7. print("test accuracy: %f" % sparse_categorical_accuracy.result())

2.预测房价 DNN  https://www.cnblogs.com/ylxn/p/10170948.html

                             https://www.kaggle.com/zoupet/neural-network-model-for-house-prices-tensorflow

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/697800
推荐阅读
相关标签
  

闽ICP备14008679号