[英]creating a one-hot for Fashion-MNIST dataset with tensorFlow
Is the below case a scenario were one should create a hot-one encodingfor the labels?以下情况是否应该为标签创建一个热编码?
I also tried to create a hot-one encoding but kept getting errors.我还尝试创建一个热门编码,但一直出错。 How is this done?
这是怎么做到的?
Note: I'm working in googles colab.注意:我在 googles colab 工作。
Thank you.谢谢你。
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
fashion = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress','Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = tf.cast(train_images, tf.float32) / 255.0
test_images = tf.cast(test_images, tf.float32) / 255.0
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10, batch_size=512, shuffle=True, validation_split=0.1)
To add the one-hot encoding I tried changing the data to:要添加 one-hot 编码,我尝试将数据更改为:
train_images = tf.cast(train_images, tf.float32) / 255.0
test_images = tf.cast(test_images, tf.float32) / 255.0
train_labels = tf.one_hot(tf.cast(train_labels, tf.int64), depth=10)
test_labels = tf.one_hot(tf.cast(test_labels, tf.int64), depth=10)
Which gave the error:这给出了错误:
InvalidArgumentError Traceback (most recent call last) in () 27 28 ---> 29 model.fit(train_images, train_labels, epochs=10, batch_size=512, shuffle=True, validation_split=0.1) 30 InvalidArgumentError Traceback (最近一次调用最后一次) in () 27 28 ---> 29 model.fit(train_images, train_labels, epochs=10, batch_size=512, shuffle=True, validation_split=0.1) 30
I think this code should work.我认为这段代码应该可以工作。 It is without one-hot encode but it works perfectly.
它没有 one-hot 编码,但它工作得很好。
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images / 255.0
test_images = test_images / 255.0
model = keras.Sequential([keras.layers.Flatten(input_shape=(28, 28)),keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(10, activation=tf.nn.softmax)])
model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=20)
I have found the answer.我找到了答案。 Please see Sparse_categorical_crossentropy vs categorical_crossentropy (keras, accuracy)
请参阅Sparse_categorical_crossentropy vs categorical_crossentropy (keras, accuracy)
To fix the code for one-hot encoding you should fix the code:要修复 one-hot 编码的代码,您应该修复代码:
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
To:至:
model.fit(train_images, one_hot_train_labels, epochs=10, batch_size=128, shuffle=True, validation_split=0.1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.