The conditions of the input validation have changed (in the newest version your input would be accepted), but what's relevant is that the error message is much more clear:
raise ValueError(
'Please provide data as a list or tuple of 1, 2, or 3 elements '
' - `(input)`, or `(input, target)`, or `(input, target,'
'sample_weights)`. Received %s. We do not use the `target` or'
'`sample_weights` value here.' % inputs.output_shapes)
[TLDR] Only insert empty dictionary as a dummy input and specify the number of steps:
model.predict(x = {}, steps = 4)
Full code:
import numpy as np import tensorflow as tf from tensorflow.data import Dataset from tensorflow.keras.layers import Dense, Input from tensorflow.keras.models import Model # dummy data: x = np.arange(4).reshape(-1, 1).astype('float32') y = np.arange(5, 9).reshape(-1, 1).astype('float32') # build the Datasets ds_x = Dataset.from_tensor_slices(x).repeat().batch(4) it_x = ds_x.make_one_shot_iterator() ds_y = Dataset.from_tensor_slices(y).repeat().batch(4) it_y = ds_y.make_one_shot_iterator() # build compile and train the model input_vals = Input(tensor = it_x.get_next()) output = Dense(1, activation = 'relu')(input_vals) model = Model(inputs = input_vals, outputs = output) model.compile('rmsprop', 'mse', target_tensors = [it_y.get_next()]) model.fit(steps_per_epoch = 1, epochs = 5, verbose = 2) # infer using the dataset model.predict(x = {}, steps = 4)
Last updated 2022-06-09 UTC.
A data source constructs a
Dataset
from data stored in memory or in one or more files.A data transformation constructs a dataset from one or more
tf.data.Dataset
objects.
import tensorflow as tf
import tensorflow as tf
import pathlib
import os
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
np.set_printoptions(precision = 4)
The Dataset
object is a Python iterable. This makes it possible to consume its
elements using a for loop:
dataset = tf.data.Dataset.from_tensor_slices([8, 3, 0, 8, 2, 1]) dataset
<TensorSliceDataset element_spec=TensorSpec(shape=(), dtype=tf.int32, name=None)>
for elem in dataset:
print(elem.numpy())
for elem in dataset:
print(elem.numpy())
8 3 0 8 2 1
Describe the current behavior When passing tf.data.Dataset instance to model.predict method which is instantiated by tf.keras.Sequential, tf.keras.Model, subclassing tf.keras.Model, return of model.predict is different from return of model.call,Facing a similar issue with LSTM in both v1.13.1 and 2.0.0-alpha0. I have tried both eager_execution and Session api. A simple code to reproduce the issue, where first predictions are same for both, but consecutive predictions are not:,Ao repetir a rotina para " yhat_from_call_method" tive como resposta: RuntimeError: get_session is not available when using TensorFlow 2.0. Existe uma rotina que faça a mesma coisa no TensorFlow 2.0?,You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
import numpy as np
import tensorflow as tf
from tensorflow
import keras
1.12 .0
x = np.random.randn(60, 30).astype(np.float32) y = np.random.randint(low = 0, high = 10, size = 60).astype(np.int32) x_tr = x[0: 20] y_tr = y[0: 20] x_val = x[20: 40] y_val = y[20: 40] x_tst = x[40: 60] y_tst = y[40: 60] print(x_tr.shape, y_tr.shape) print(x_val.shape, y_val.shape) print(x_tst.shape, y_tst.shape) tr_dataset = tf.data.Dataset.from_tensor_slices((x_tr, y_tr)) tr_dataset = tr_dataset.batch(batch_size = 4).repeat() val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val)) val_dataset = tr_dataset.batch(batch_size = 4).repeat() tst_dataset = tf.data.Dataset.from_tensor_slices((x_tst, y_tst)) tst_dataset = tst_dataset.batch(batch_size = 4) print(tr_dataset) print(val_dataset) print(tst_dataset)
(20, 30) (20,)
(20, 30) (20,)
(20, 30) (20,)
<RepeatDataset shapes: ((?, 30), (?,)), types: (tf.float32, tf.int32)>
<RepeatDataset shapes: ((?, ?, 30), (?, ?)), types: (tf.float32, tf.int32)>
<BatchDataset shapes: ((?, 30), (?,)), types: (tf.float32, tf.int32)>
class Model(keras.Model):
def __init__(self, num_classes):
super(Model, self).__init__()
self.dense = keras.layers.Dense(units = 10, activation = 'softmax')
def call(self, inputs):
score = self.dense(inputs)
return score
model = Model(10)
model.compile(optimizer = tf.train.GradientDescentOptimizer(.1),
loss = keras.losses.sparse_categorical_crossentropy)
model.fit(tr_dataset, epochs = 5, steps_per_epoch = 20 //4,
validation_data = val_dataset, validation_steps = 20 //4)
Epoch 1 / 5
5 / 5[ === === === === === === === === === === ] - 0 s 36 ms / step - loss: 2.5626 - val_loss: 1.9200
Epoch 2 / 5
5 / 5[ === === === === === === === === === === ] - 0 s 2 ms / step - loss: 1.9345 - val_loss: 1.4256
Epoch 3 / 5
5 / 5[ === === === === === === === === === === ] - 0 s 2 ms / step - loss: 1.4506 - val_loss: 1.0624
Epoch 4 / 5
5 / 5[ === === === === === === === === === === ] - 0 s 1 ms / step - loss: 1.0906 - val_loss: 0.8049
Epoch 5 / 5
5 / 5[ === === === === === === === === === === ] - 0 s 2 ms / step - loss: 0.8314 - val_loss: 0.6274