315448 (3) [Avatar] Offline
#1
When executing the fit() function following the IMDB dataset example on page 66, I receive the error message, "ValueError: if fitting from data tensors, you should specify the steps_per_epoch" argument. Whatever integer value I supply for that argument, such as 20 (the number of samples [10000] divided by the batch size [512]), I receive the error message, "AttributeError: 'NoneType' object has no attribute 'shape'."

My R session is running Keras package version 2.1.5 and tensorflow package version 1.5
256385 (46) [Avatar] Offline
#2
I'm not able to reproduce this error (even running the exact same versions of Keras and TF that you are). Could you provide a complete script along with sessionInfo() ?
315448 (3) [Avatar] Offline
#3
As requested, attached is a screenshot of the R Console session info. And here is the R script, modeled after the script in the book that begins on page 60, just the names of the variables have been changed to more descriptive ones.

library(‘keras’)
imdb.data <- dataset_imdb(num_words = 10000)

training.set.data <- imdb.data$train$x ; fetch the predictor variables
training.set.labels <- imdb.data$train$y ; fetch the predicted variable

testing.set.data <- imdb.data$test$x ; fetch the predictor variables
testing.set.labels <- imdb.data$test$y ; fetch the predicted variable

Create a custom function that accepts a group of lists then returns a tensor array with one row for each list, populated with the values of each list.

create_tensor <- function(incoming.lists, values.per.list) {
results <- matrix(0, nrow = length(incoming.lists), ncol = values.per.list)
for (ii in 1:length(incoming.lists))
results[ii, incoming.lists[[ii]]] <- 1
}
}

Convert the labels (predicted values) from integers to numeric values (required by Keras functions).

training.set.labels <- as.numeric(training.set.labels)
testing.set.labels <- as.numeric(testing.set.labels)

Create a Keras-based model that has three layers (Input, Middle, and Output); the first two layers will contain 16 neurons apiece while the Output Layer has just a single neuron.

trained.model <- keras_model_sequential() %>%
layer_dense(units = 16, activation = 'relu', input_shape = c(10000)) %>%
layer_dense(units = 16, activation = 'relu') %>%
layer_dense(units = 1, activation = 'sigmoid')

compile(trained.model, optimizer = ‘rmsprop’, loss = ‘binary_crossentropy’, metrics = c(‘accuracy’))

Create a validation set to enable the training algorithm to test the accuracy of its predictions as it trains the model. Assign the first 10,000 rows of the Training Set to the validation set, then the remaining rows will be used for training.

validation.row.numbers <- 1:10000

validate.x <- training.tensor[validation.row.numbers, ]
train.x <- training.tensor[-validation.row.numbers, ]

validate.y <- testing.tensor[validation.row.numbers, ]
train.y <- testing.tensor[-validation.row.numbers, ]

Train the model (in batches of 512 rows) for 20 iterations/epochs.

fit(trained.model, train.x, train.y, epochs = 20, batch_size = 512,
validation_data = list(validate.x, validate.y))

Executing the fit() command causes the "steps_per_epoch" error message I described earlier in this posting.

256385 (46) [Avatar] Offline
#4
That code doesn't actually run for me, there is no object named training.tensor so we get:

> validate.x <- training.tensor[validation.row.numbers, ]	
Error: object 'training.tensor' not found


315448 (3) [Avatar] Offline
#5
My typing left out that line -- my mistake. There should be these additional lines just after the create_tensor() function that invoke that function twice.
-------------------

Now use the custom function to convert the 10,000 values in the Training Set & Testing Set into tensor arrays.

training.tensor <- create_tensor(training.set.data, 10000)
testing.tensor <- create_tensor(testing.set.data, 10000)
256385 (46) [Avatar] Offline
#6
The function create_tensor has no return value so it returns NULL (and this results in your subsequent errors).

547965 (1) [Avatar] Offline
#7
Sorry,I also have similar problems in this section 3.4.4.
I want to monitor loss and accuracy on the 10,000 samples that I set apart.
(In my source code,the separated samples are called
"x_val" & "partial_x_train", "y_val" & "partial_y_train")

But I receive the error message, about "validation_steps"
I really want to know the reason why I get error messages about "validation_steps".

My R session is running Keras package version 2.1.5
and tensorflow package version 1.5.


This is my source code
-----------------------------------start
library(keras)

#import dataset
N_W <- 10000
imdb <- dataset_imdb(num_words = N_W)
c(c(train_data,train_labels),c(test_data,test_labels)) %<-% imdb

#transfar from list to tensor
create_tensor <- function(sequences,dimension = N_W){
results <- matrix(0,nrow = length(sequences),ncol = dimension)
for(i in 1:length(sequences)){
results[i,sequences[[i]]] <- 1
}
return(results)
}

#datasets
x_train <- create_tensor(train_data,dimension = N_W)
x_test <- create_tensor(test_data,dimension = N_W)
y_train <- as.numeric(train_labels)
y_test <- as.numeric(test_labels)


#traning model
model <- keras_model_sequential() %>%
layer_dense(units = 16,activation = "relu",input_shape = c(N_W)) %>%
layer_dense(units = 16,activation = "relu") %>%
layer_dense(units = 1,activation = "sigmoid")

#compiling the model
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("accuracy")
)

#Setting aside a validation set
val_indices <- 1:10000
x_val <- x_train[val_indices,]
partial_x_train <- x_train[-val_indices,]
y_val <- y_train[val_indices]
partial_y_train <- y_train[-val_indices]


#training --validation--
history <- model %>% fit(
partial_x_train,
partial_y_train,
epochs = 20,
batch_size = 512,
validation_data = list(x_val,y_val)
)

--------------------------------------end