plot() does not work with keras, eclipse and statET - r

I am running keras library from R under eclipse / statET / windows 10. After to fit a model, I am trying to run the following code:
history <- model %>% fit_generator(train_generator, steps_per_epoch = 100, epochs = 30, validation_data = validation_generator, validation_steps = 50)
jpeg(filename="figuras/history.jpg")
plot(history)
dev.off()
But no figure is drawn. Nevertheless, if I execute the same sententes above shown in the command line of statET, or I run the whole script from R, it works fine: could someone explain such behaviour?

Related

input error when fitting model Neural network in R

I get an error when i try to run this code. i followed a guide on youtube for building a neural network. Everything works except when i try to run this code to fit the model.
history <- modnn %>% fit(
train_X_matrix, train_Y, epochs = 50, batch_size = 600,
validation_data = list(validation_X_matrix,validation_Y))
```
the error i get when i try to run the code above, all the names you see are the names of the columns. So the features of the model:
[error in visual studio](https://i.stack.imgur.com/ZaPXw.png)
some extra info about the variables i use. Here i created a matrix of the input variables. They did this in the guide. I tried train_x_data as input then it gave the same error but immediately so not after 1 epoch
```
# dependent and independent variables in 1 dataframe
train_X_data <- data.frame(train_X,train_y)
validation_X_data <- data.frame(validation_X,validation_y)
train_X_matrix <- model.matrix(average_daily_rate ~. -1 , data = train_X_data)
train_Y <- train_X_data$average_daily_rate
validation_X_matrix <- model.matrix(average_daily_rate ~. -1, data = validation_X_data)
validation_Y <- validation_X_data$average_daily_rate
```
The model i use, it is just a simple single layer model for testing.
# 1) single layer model structure
# step 1 make architecture powerful enough
modnn <- keras_model_sequential() %>%
layer_dense(units = 500, activation = "relu",
input_shape = ncol(train_X)) %>%
layer_dense(units = 1)
summary(modnn)
modnn %>% compile(loss = "mse",
optimizer = optimizer_rmsprop(),
metrics = list("mean_absolute_error"))
The error occurs after running the first epoch. I tought it was because the model could not read the names of the columns, but i tried a lot of things and nothing seemed to work.
Does anyone have an idea on how to fix this?

Cannot get Tensorboard visualization tool to open

I cannot get the TensorBoard to open using RStudio and Keras package.
I am trying to duplicate the TensorBoard using the Keras package with R Studio as shown here: https://tensorflow.rstudio.com/tools/tensorboard/tensorboard/
Either I have a problem or I don't understand what needs to be done.
I am using these instructions:
# launch TensorBoard (data won't show up until after the first epoch)
tensorboard("logs/run_a")
# fit the model with the TensorBoard callback
history <- model %>% fit(
x_train, y_train,
batch_size = batch_size,
epochs = epochs,
verbose = 1,
callbacks = callback_tensorboard("logs/run_a"),
validation_split = 0.2
)
I get this error after running tensorboard("logs/run_a"):
> tensorboard("logs/run_a")
Error in if (tensorboard_version() < "2.0") { :
argument is of length zero
I have tried these versions:
tensorboard("/Users/kevinwilliams/Documents/r-studio-and-git/MNIST/logs/run_a")
tensorboard("logs/run_a")
tensorboard(log_dir = "logs/run_a")
tensorboard(log_dir = "logs/run_a", launch_browser = TRUE)`
The TensorBoard will not open.
The files structure of "logs" and "logs/run_a" was automatically created by these commands.
Training and Validation "events" are being saved to the file locations.
"fitting" the model does run and execute with no error. The output is sent to the RStudio Viewer and not to the TensorBoard.
Keras V2.7.0
RStudio 1.4.1717
R 4.1.1
It is just a file logging me even plots with matlibplot 👈👈👈
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1]) # 0.5 - 1
plt.legend(loc='lower right')
Can you see there is some logging generated at target folder where you specific or checkpoints when you read it.?
Only using a tf.profile services to communicate with sub-services they already listening.
options = tf.profiler.experimental.ProfilerOptions(host_tracer_level = 3,
python_tracer_level = 1, device_tracer_level = 1)
tf.profiler.experimental.start(log_dir, options = options)
tf.profiler.experimental.server.start(6009)
tf.profiler.experimental.stop()

R.keras error during call to fit function

I am new to R keras, so please bear with me. I am trying to build a simple model using variables that are categorical, but I've recast as numeric.
I can get examples working from various tutorials in R/keras with my current installation so I know its not in reticulate or tensorflow or even R. However, when I try to use my own data to create the simple model, I obtain the following errors during the "fit" execution:
I'm pretty sure its my training data format, but I cannot for the life of me figure out what is going wrong. Thank you kindly in advance.
# Fit
model_one <- model %>%
+ fit(training,
+ trainLabels,
+ epochs = 100,
+ batch_size = 32,
+ validation_split = 0.2)
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: in user code:
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\keras\engine\training.py:571 train_function *
outputs = self.distribute_strategy.run(
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\keras\engine\training.py:533 train_step **
y, y_pred, sample_weight, regulari
I've upload my script and sample data file to github:
Sample Data and Script to reproduce error
Actually, found the error:
One hot encoding generates a 2 column matrix:
# One hot encoding
trainLabels <- to_categorical(trainingtarget)
testLabels <- to_categorical(testtarget)
print(testLabels[1:10,])
and the model was expecting 3 columns.
I changed the model call to automatically accept the correct number of columns based on the variables instead:
model %>%
layer_dense(units = 8, activation = 'relu', input_shape = ncol(test)) %>%
layer_dense(units = ncol(trainLabels), activation = 'softmax')

executing keras CNN models keeps crashing my computer

I'm trying to get into deep learning with R. Using various blogs online I'm trying to test their code and see how they actually work. With keras, I'm not sure why but everytime I run a modelfunction It keeps crashing.
I'm sorry if I haven't provided enough information. I'm running an AMD GPU and CPU
Example code section
history <- model %>% fit_generator(
train_generator,
steps_per_epoch = 100,
epochs = 100,
validation_data = validation_generator,
validation_steps = 50,
)
use_multiprocessing=False
an also
hist <- model %>% fit_generator(
# training data
train_image_array_gen,
# epochs
steps_per_epoch = as.integer(train_samples / batch_size),
epochs = epochs,
# validation data
validation_data = valid_image_array_gen,
validation_steps = as.integer(valid_samples / batch_size),
# print progress
verbose = 2,
callbacks = list(
# save best model after every epoch
callback_model_checkpoint("C:/Users/My Account/Desktop/fruits_checkpoints.h5", save_best_only = TRUE),
)
It looks like the problem is with Keras using tensorflow-gpu. Try running the model after installing tensorflow cpu version. Since, you are using AMD gpu, you may not be able to use the tensorflow gpu version with cudnn libraries.

Export Keras Training History Plot inside Loop

I'm training a Keras model in R inside a for-loop (yes I know, for-loops bad). While plot(history) for the model works perfectly when I run the code one iteration at a time, it outputs a blank image when inside the loop.
Since this uses ggplot2, I've tried to set a delay via Sys.sleep(2) to see if there was some system lag associated with the plotting but it didn't help.
hist_nm_pre <- model %>% fit(
x_fake, y_fake,
batch_size = 500,
epochs = transfer_epochs,
validation_data = list(xy$x_val, xy$y_val),
shuffle = TRUE
)
png(file=sprintf("output/iter_%d_pre.png", i), width=1200, height=800)
plot(hist_nm_pre)
Sys.sleep(2)
dev.off()
Is there a different way I should be exporting these plots? Or is this a bug farther up the stack?

Resources