0
$\begingroup$

The keras digit object detection code does actually run in Jupyter using the R kernel.

But it took three attempts to recognize that there wasn't an issue with Jupyter, or the R kernel, or the code. The problem was mistakenly thinking there was "an issue" because Jupyter was not outputting the Epoch results.

I closed the Notebook twice and went back to scratch because it seemed all too likely that I'd made some mistake or another.

Then I found that missing Epoch output being displayed in the PowerShell terminal - the same terminal I'd used to start the Jupyter Notebooks. Turns out there was nothing actually wrong with the IDL. And the predictions output does get displayed in Jupyter after some waiting.

So this question is: How to display the output into the Jupyter output cells when using keras in Jupyter?

library("keras")

mnist = dataset_mnist()
mnist$train$x <- mnist$train$x/255
mnist$test$x <- mnist$test$x/255

model <- keras_model_sequential() %>% 
  layer_flatten(input_shape = c(28, 28)) %>% 
  layer_dense(units = 128, activation = "relu") %>% 
  layer_dropout(0.2) %>% 
  layer_dense(10, activation = "softmax")

model %>% 
  compile(
    loss = "sparse_categorical_crossentropy",
    optimizer = "adam",
    metrics = "accuracy"
  )

model %>% 
  fit(
    x = mnist$train$x, y = mnist$train$y,
    epochs = 5,
    validation_split = 0.3,
    verbose = 2
  )

predictions <- predict(model, mnist$test$x)
head(predictions, 2)

model %>% 
  evaluate(mnist$test$x, mnist$test$y, verbose = 0)

And the output from the PowerShell terminal;

c:\program files\python39\lib\site-packages\notebook\services\kernels\kernelmanager.py:176> cb=[IOLoop.add_future.<locals>.<lambda>() at c:\program files\python39\lib\site-packages\tornado\ioloop.py:695]> is being executed.
[I 17:46:07.080 NotebookApp] Kernel started: 758be753-4e56-41e9-b628-e3e66136f7ee, name: ir36
[W 17:46:10.900 NotebookApp] Timeout waiting for kernel_info reply from 60ec78bf-773a-40df-b19a-b44dae383872
[E 17:46:10.901 NotebookApp] Error opening stream: HTTP 404: Not Found (Kernel does not exist: 60ec78bf-773a-40df-b19a-b44dae383872)
[I 17:46:14.815 NotebookApp] Adapting from protocol version 5.0 (kernel 758be753-4e56-41e9-b628-e3e66136f7ee) to 5.3 (client).
[I 17:48:06.974 NotebookApp] Saving file at /R_TensorFlow/Untitled.ipynb
[W 17:48:07.484 NotebookApp] Trusting notebook /R_TensorFlow/Untitled.ipynb
[I 17:48:07.846 NotebookApp] Saving file at /R_TensorFlow/Untitled.ipynb
[I 17:48:08.771 NotebookApp] Starting buffering for 758be753-4e56-41e9-b628-e3e66136f7ee:b4e1466d1f7448c08db2851842d80ba9
[I 17:48:09.598 NotebookApp] Adapting from protocol version 5.0 (kernel 758be753-4e56-41e9-b628-e3e66136f7ee) to 5.3 (client).
2021-11-08 17:49:45.432220: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-11-08 17:49:52.419047: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 2818 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 750 Ti, pci bus id: 0000:01:00.0, compute capability: 5.0
Epoch 1/5
[I 17:50:09.435 NotebookApp] Saving file at /R_TensorFlow/Untitled.ipynb
1313/1313 - 15s - loss: 0.3407 - accuracy: 0.9005 - val_loss: 0.1809 - val_accuracy: 0.9464 - 15s/epoch - 11ms/step
Epoch 2/5
1313/1313 - 8s - loss: 0.1649 - accuracy: 0.9518 - val_loss: 0.1364 - val_accuracy: 0.9602 - 8s/epoch - 6ms/step
Epoch 3/5
1313/1313 - 8s - loss: 0.1218 - accuracy: 0.9635 - val_loss: 0.1091 - val_accuracy: 0.9676 - 8s/epoch - 6ms/step
Epoch 4/5
1313/1313 - 8s - loss: 0.1001 - accuracy: 0.9691 - val_loss: 0.1036 - val_accuracy: 0.9691 - 8s/epoch - 6ms/step
Epoch 5/5
1313/1313 - 8s - loss: 0.0814 - accuracy: 0.9750 - val_loss: 0.0986 - val_accuracy: 0.9723 - 8s/epoch - 6ms/step
[I 17:52:09.440 NotebookApp] Saving file at /R_TensorFlow/Untitled.ipynb
[I 17:54:09.416 NotebookApp] Saving file at /R_TensorFlow/Untitled.ipynb
Epoch 1/5
1313/1313 - 8s - loss: 0.0717 - accuracy: 0.9778 - val_loss: 0.0941 - val_accuracy: 0.9728 - 8s/epoch - 6ms/step
Epoch 2/5
1313/1313 - 8s - loss: 0.0638 - accuracy: 0.9791 - val_loss: 0.0911 - val_accuracy: 0.9738 - 8s/epoch - 6ms/step
Epoch 3/5
1313/1313 - 8s - loss: 0.0551 - accuracy: 0.9818 - val_loss: 0.0917 - val_accuracy: 0.9737 - 8s/epoch - 6ms/step
Epoch 4/5
1313/1313 - 8s - loss: 0.0496 - accuracy: 0.9836 - val_loss: 0.0926 - val_accuracy: 0.9744 - 8s/epoch - 6ms/step
Epoch 5/5
1313/1313 - 8s - loss: 0.0463 - accuracy: 0.9849 - val_loss: 0.0896 - val_accuracy: 0.9754 - 8s/epoch - 6ms/step
[I 17:56:09.425 NotebookApp] Saving file at /R_TensorFlow/Untitled.ipynb
[E 18:40:48.511 NotebookApp] Kernel info request failed, assuming current {}
$\endgroup$
0
0
$\begingroup$

It looks that it is a currently unsolved issue: https://github.com/IRkernel/IRkernel/issues/416. In keras the result of fit() is the training history, so you can assign it to a variable (history <- model %>% fit (..)) and inspect or plot it (plot(history)) after the training, as in the example here. If you need interactive output, you can run your code in the terminal with Rscript or R.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.