question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValueError on first run

See original GitHub issue

Hey there,

I was following the README code snippet and ran into a bit of trouble. My code is word-for-word is being run on MNIST.

import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from kerastuner.tuners import RandomSearch

(xtrain, ytrain), (xtest, ytest) = mnist.load_data()
xtrain = xtrain.reshape([-1, 784, 1]) / 255
xtest = xtest.reshape([-1, 784, 1]) / 255

ytrain = tf.keras.utils.to_categorical(ytrain, 10)
ytest = tf.keras.utils.to_categorical(ytest, 10)

print (xtrain.shape, ytrain.shape)

def build(hp):
	model = tf.keras.models.Sequential()
	model.add(Dense(units=hp.Int('units', min_value=32, max_value=512, step=32), activation="relu"))
	model.add(Dense(10, activation="softmax"))

	model.compile(loss="sparse_categorical_crossentropy", optimizer=Adam(hp.Choice("learning_rate", values=[1e-2, 1e-3, 1e-4])), metrics=["accuracy"])

	return model

tuner = RandomSearch(build, objective='val_accuracy', max_trials=5, executions_per_trial=3, directory="bin", project_name="tuner_test")

tuner.search_space_summary()
tuner.search(xtrain, ytrain, epochs=10, validation_data=(xtest, ytest))
model = tuner.get_best_models(num_models=5)
tuner.results_summary()

I keep getting this error, causing my build to fail.

ValueError: Shape mismatch: The shape of labels (received (32, 10)) should equal the shape of logits except for the last dimension (received (32, 784, 10)).

Any help would be greatly appreciated 😃

Cheers!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
mickeyhscommented, Mar 14, 2020

just found the issue of mismatch change loss=“sparse_categorical_crossentropy” to loss=“categorical_crossentropy” will work. since the label had called to_categorica()

1reaction
rish-16commented, Feb 5, 2020

I’ll go take a look. Appreciate your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValueError: need more than 1 value to unpack - Stack Overflow
I have tried running the program through the Windows command prompt, to no avail. I have also run the program by typing: "python...
Read more >
Python ValueError Exception Handling Examples - DigitalOcean
Python ValueError is raised when a function receives an argument of the correct type but an inappropriate value. Also, the situation should not ......
Read more >
compressai.entropy_models.entropy_models - CompressAI
Run update() first") if len(self._cdf_length.size()) != 1: raise ValueError(f"Invalid offsets size {self._cdf_length.size()}") def compress(self, inputs, ...
Read more >
Better performance with tf.function | TensorFlow Core
You may encounter ValueError: tf.function only supports singleton tf.Variables created on the first call. when using more than one Keras optimizer with a...
Read more >
Beginning Python 3 By Doing #9 - ValueError, try, except, finally
Docker Tutorial for Beginners - A Full DevOps Course on How to Run Applications in Containers. freeCodeCamp.org. freeCodeCamp.org.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found