1

I'm working with R studio v1.2.1335 (r markdown and reticulate package), Python 3.7.4 installed in environment Anaconda 4.7.10.

Struggling with this code (https://www.guru99.com/keras-tutorial.html)

```{python script 1}    
import keras
    from keras.models import Sequential
    from keras.layers import Dense, Activation
    import numpy as np
    import matplotlib.pyplot as plt 

    x = data = np.linspace(1,2,200)
    y = x*4 + np.random.randn(*x.shape) * 0.3


    model = Sequential()
    model.add(Dense(1, input_dim=1, activation='linear'))

    model.compile(optimizer='sgd', loss='mse', metrics=['mse'])

    weights = model.layers[0].get_weights()
    w_init = weights[0][0][0]
    b_init = weights[1][0]
    print('Linear regression model is initialized with
    weights w: %.2f, b: %.2f' % (w_init, b_init)) 


    model.fit(x,y, batch_size=1, epochs=30, shuffle=False)

    weights = model.layers[0].get_weights()
    w_final = weights[0][0][0]
    b_final = weights[1][0]
    print('Linear regression model is trained to have
    weight w: %.2f, b: %.2f' % (w_final, b_final))

    predict = model.predict(data)

    plt.plot(data, predict, 'b', data , y, 'k.')
    plt.show()
```

error:

Evaluation error: option error has NULL value.

the same error happens with other code that use keras and comand Dense.

others info with py_config()

python:         C:\Users\Megaport\Anaconda3\envs\r-reticulate\python.exe
libpython:      C:/Users/Megaport/Anaconda3/envs/r-reticulate/python37.dll
pythonhome:     C:\Users\Megaport\ANACON~1\envs\R-RETI~1
version:        3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\Megaport\ANACON~1\envs\R-RETI~1\lib\site-packages\numpy
numpy_version:  1.17.2

python versions found: 
 C:\Users\Megaport\Anaconda3\envs\r-reticulate\python.exe
 C:\Users\Megaport\ANACON~1\python.exe
 C:\Users\Megaport\Anaconda3\python.exe
 C:\Users\Megaport\Anaconda3\envs\venv\python.exe

-------------------- UPDATE ------------------- I've run the script in Rstudio without r-markdown but only with python and it worked without errors. The problem must be in r-markdown or reticulate installation.

Any help would be appreciated

Mirko Piccolo
  • 319
  • 1
  • 2
  • 12
  • Aren't you mixing python and R? – Daniel Möller Sep 07 '19 at 22:09
  • With reticulate and r-markdown I can run python script in R – Mirko Piccolo Sep 07 '19 at 22:12
  • You cannot break lines like your `print` statements unless using triple-quote strings. Plus, you have indentation issues. Remember Python is a strong-type language with certain syntax rules unlike R. Also, the modulo operator, `%`, for string interpolation is not recommeded, currently [de-emphasised, (but not officially deprecated *yet*)](https://stackoverflow.com/a/13452357/1422451). Use `str.format` instead. – Parfait Sep 08 '19 at 00:19
  • 2
    Even though you can use R, I think you would benefit from using an actual python interpreter/IDE, so you can see the errors in the Python code properly, see the actual error messages and at which line they occur. – Daniel Möller Sep 08 '19 at 05:35
  • @DanielMöller Good idea, I've edited the post. The script works running only python in R. – Mirko Piccolo Sep 08 '19 at 07:26

0 Answers0