Deep Neural Networks with TensorFlow & Keras in R | Numeric Response Variable
Vložit
- čas přidán 30. 05. 2024
- Provides steps for applying deep neural networks for numeric response or independent variable.
R file: goo.gl/MwBLVt
Machine Learning videos: goo.gl/WHHqWP
For citation as reference in a research paper, use:
Reference: Rai BK, (2019). “Advanced Deep Learning with R: Become an expert at designing, building, and improving advanced neural network models using R”, Packt Publishing.
Timestamps:
00:00 Introduction
01:46 Neural Network Visualizations
03:44 Matrix conversion and Data Partition
05:13 Normalize
06:24 Create Model
07:44 Compile
08:15 Fit Model
09:45 Evaluate
11:21 Fine Tune Model
14:04 Improvements
Deep learning with neural networks is an important tool related to analyzing big data or working in data science field.
R is a free software environment for statistical computing and graphics, and is widely used by both academia and industry. R software works on both Windows and Mac-OS. It was ranked no. 1 in a KDnuggets poll on top languages for analytics, data mining, and data science. RStudio is a user friendly environment for R that has become popular.
One of the best tutorials I ever watched, Thank you so much, you are awesome!
Thanks for your comments and feedback!
Dr. Rai I wish to thank you deeply for these easy explanations about NN. Again thank you very much
You are most welcome!
Your videos are amazing. Thank you for exposing us to your knowledge.
Thanks for comments!
Fantastic video. Thank you so much Dr Rai.
Thanks for your comments!
You just made my work easier, thank you very much indeed!
Glad to hear that!
Recently found your video's. Thank you so much for doing these, they are clear and thorough. You sir are my new best friend.. :)
Welcome aboard!
Thank you, I've learned a lot watching your videos ! Please can you do a tutorial to build a recurrent neural network (RNN) on R for data prediction ? An LSTM/GRU one would be awesome !
Thanks for the suggestion. I'm seeing this very late, but it should happen in about a month.
@@bkrai Thank you Sir !
Thank you so much, this helped me a lot! You explained everything really well
You're very welcome!
Dear Rai,You have made a series of outstanding deep learning videos, which helped me a lot. Thank you for your contribution and look forward to your more instructional videos on deep learning ( Long short-term memory network)
Thanks! I'll soon do LSTM too.
@@bkrai Looking forward to your next video
Thanks!
Very useful and very well explained!
Thanks for comments!
Simple language u explained sir.Thank you for making video
You are most welcome!
Thank you so much, you actually helped me sir. LOVE YOU !!!!!
Thanks for comments!
Great video as usual. As time permits love to see a video on AutoEncoders. Thank you
Thanks for the suggestion, I've added it to my list.
Great video, thank you. I am having problem un-scaling the prediction value using unscale function (Error: Error in scale.default(data, center = FALSE, scale = 1/scale) : length of 'scale' must equal the number of columns of 'x'). Is there a way to unscale with keras function or some other way?
Thank you very much Dr. Rai for your informative videos. Your tutorials are like compressed 3 months of work in 20 minutes!!! Is there a chance you could post a multivariate forecasting and tuning example with LSTM.
Thank you for a very clear and interesting video! About overfitting: should you stop when train and test error curves cross or can you keep training? What if both errors are still decreasing with continued iteration? Is the danger of over fitting mainly in thinking the training error is valid for the model applied to new data?
thank you for this helpful tuto and please we need more tuto for using keras in deep leruning for regression case
Thanks for the suggestion, I've added this to my list.
you rock, Dr. Rai.
Thanks!
Hi, thanks for the great tutorials. Can this model be used with and ordinal dependent variable. Like a horse race first, second, third, lose?
Thanks again.
I'm digging the music professor!
:)
One of the best and simple explanation of DNN. Can you please make a tutorial on time series forecasting using a combination of DL models such as CNN-LSTM?
Thanks, I've added it to my list.
Thank you for great explanation, could you help me with this error in model creation
model1
Great video as always. Does this model produce a linear equation (similar to lm.fit) that can be extracted out?
For calculations with a simple example, you may refer to this link:
czcams.com/video/-Vs9Vae2KI0/video.html
All your videos are very informative and everything is so well explained. I have been working on time series data. Your video on facebook's prophet library was amazing, although prophet only works well on an ideal dataset. I have a request sir, please do a tutorial for time series forecasting using lstm in R, would be really helpful
Thanks for comments and suggestion! I've added it to my list.
Sir, I need to calculate rsquared value of the model. How can I get the r squared or accurate ration?
Dear Dr. Rai. thanks a lot for your informative video. I am trying to install keras in r on Mac Pro M1 . but it does not work and gives a fatal error each e=time I run the code. I used your video as an examples for my data. may I ask you if you have a step by step install and configure keras and tensforflow on Mac Pro M1?
Thanks in advance!
Great Video. Question: In the other video you used the min max normalization, would it work here too? How do I know which normalization method to use? Thanks
It is important to do normalization. However there may not be much difference when it comes to model performance. So any one of the 2 methods will work fine.
Am curious, when you applied the training's mean and sd for the Test's centre and scale, are'nt you introducing data leakage in this case?
This will minimize data leakage.
Hi sir, thanks for your videos.
May I ask you why you set to NULL (ie V1, V2 etc) the names of the columns?
Thanks.
I wanted to have simple variable names such as V1, V2, .. etc.
Prof., understood the principles. Excellent lecture. Could you please let us know how to use the selected model for forecasted vales for future period. Regards
Dr Rai, Thankyou very much for sharing your knowledge & educating the data science professionals,
quick question, What would be your recommendation for saving trained Model & exporting to other machines , formats
Something like JSON, or XML, Or CSV (more generic), or do we need to stick to just '.RDS' ?
RDS should work fine.
Nice job. Clean and crisp!
Thanks for comments!
Also, curious when calling Tensorflow from Keras R, is it running inside R memory space or is it running in its own memory space / remote machine? Thank you...
It runs inside R.
Great Video!
Are there any current ways to plot the Keras model similar to how the neural net package plotted the original node map? I've been doing some research on this, and I'm coming up empty handed.
I'll look into this.
Awesome video. Thank you. Two quick questions
1. Curious why you remove all the column names?
2. For converting chas shouldn't you be creating two columns using to_categorical(...) similar to what you did in one of your previous videos?
Thank you
1. This is not mandatory. I had done this for other purpose and kept the same code for this example.
2. Chas has only 2 levels, so one column with 0 and 1 values is fine here. We can use that earlier method when a factor variable has more levels.
Thank you so much for your useful tutorials. A quick question how we can obtain an exactly same result for the same model after running several times. I tried with (set.seed(1234)) but the results were different. Could you provide any suggestion for that?
It is a bit complex process. It will be easier to simply save the model.
I have a problem when I try to run the model at 7:40. The error says : Error in py_call_impl(callable, call_args$unnamed, call_args$named) :
ValueError: Only input tensors may be passed as positional arguments. The following argument value should be passed as a keyword argument: (of type )
Run `reticulate::py_last_error()` for details.
Can you help me ?
It was a very good explanation
Thanks for comments!
Dear Professor. My question is that Why every time the model gives different performance results without changing any parameters in the model?
I am curious why you choose Adam versus RMSPROPS. Also for the learning rate, is there any guidance on how to pick the learning rate? Thank you for your help.
It may be better to try both. For learning rate, it is better to start with a lower value and gradually increase to find the best level.
Thank you very much, this is very clear and helpful. Do you maybe also have a similar tutorial related to the one with multiple numeric responses?
I've added it to my list. Thanks!
Could you explain how to run dNN using raster data?
Thank you sir for your nice video about deep learning method. Kindly suggest how to extract the k-fold 'cv' results. It will be very much helpful for better presentation of results. Kindly suggest Sir
Thank you. I have learned a lot from this video.
by the way, how can I reproduce the result of the neural network every time I run the model?
Thank you so much.
That’s a bit complicated. It is suggested to run a model several times and then take average of results.
.it's backpropogation neural network which becomes more acurate and smarter every time we train it
Thank you for the video. If the factor variable has more than two levels, will we use dummy coding or something else?
You can refer to this example where response has 3 levels:
czcams.com/video/4KfiQRqn_vA/video.html
Thank yoy very much Dr. Rai , I am getting this message "your cpu supports instructions that this tensorflow binary was not compiled to use avx2" can you please help me ?
It could be a computer specific issue. You can try rstudio cloud:
czcams.com/video/SFpzr21Pavg/video.html
Thank you very much for such a nice tutorial. I have a question though, I want to make a neural network with custom connections between the neurons of the input and hidden layers, how to do that ? Basically, I am trying to apply prior knowledge to the neural net architecture and remove all those connections that do not satisfy the particular conditions. Please indicate a source or any other tutorial where I can find something like this. Many thanks in advance.
See if this helps:
www.r-bloggers.com/2020/07/creating-custom-neural-networks-with-nnlib2rcpp/
Thank you very much! I learned a lot from this video.
By the way, how do I choose the number of hidden layers before running the model each time?
Thank you very much.
Hyperparameter tuning is needed. See link below:
czcams.com/video/FscOZT0_ObA/video.html
Thank you Sir, this video is very useful to me...Sir, in different datasets how can we know whether a variable is dependent or independent?
When you go over several examples, it will become more clear.
Thank you for your video. Is there any reason using the command ' dimnames(data)
Just wanted to remove dimension names. But if it works without this line, you can ignore.
@@bkrai Thank you for your reply😍
Sir,
very nicely explained tutorial..thanks.
requested you to kindly make tutorial for multi target regression model in R-keras. in advance thanks.
Thanks, I've added it to my list.
Again Dr. Bharatendra, Do you know why I am getting an error when I am creating the model.
Please, guide me otherwise I am can't go forward using this code
"model %>%
+ layer_dense(units = 5, activation = 'relu', input_shape = c(13)) %>%
+ layer_dense(units = 1)
Error in eval(lhs, parent, parent) : object 'model' not found"
The error is either in this or previous line of code.
Hi!
Congrats for the great video!
Do you have any recommendation of packages to use in a recurrent neural network for classification purposes?
If you have a tutorial for that it is going to be amazing!
Thanks!
I have added RNN to my list for future.
when I execute the line model
was there a fix? i am facing the same issue!
There could be some computer specific issue. You can try RStudio cloud that runs from the browser,
czcams.com/video/SFpzr21Pavg/video.html
Try RStudio cloud that runs from the browser,
czcams.com/video/SFpzr21Pavg/video.html
install_keras()
tensorflow::install_tensorflow(method = "conda",version = "2.0.0").
How someone can get prediction value (dependent) for independent variable values with model for future? I think many newbies are wondering how to get an estimation after model is ok. Any help?
Line 68 in the video does prediction using the model and store the values in "pred".
@@bkrai can you provide steps how to unscale.........having issues with unscaling using unscale() function
Thx a lot 👍
You are welcome!
This very interesting but complex process was simplified by this video. Easy to understand and replicate. What about forecasting? how does one forecast the prediction for future dates?
For forecasting deep learning network LSTM is very popular. Probably I'll do a lecture video on it some time.
@@bkrai looking forward to it.
Thanks!
Quick question, When does one use Tensorflow Estimator API for regression problems versus Keras? Thank you
For key differences, see: tensorflow.rstudio.com/tensorflow/
Please tell me what is the difference between the two method I.e one solved by neuralnet package and second with keras package.
For deep networks go with keras as it will run much faster.
@@bkrai I have install keras and tensorflow bit is not working
install.packages("keras")
install_keras()
install.packages("tensorflow")
install_tensorflow()
miniconda package also installed .All library are also loaded
But I am getting this error
> model - keras_model_sequential()
Error: Installation of TensorFlow not found. Python environments searched for 'tensorflow' package: C:\Users\User\AppData\Local
-miniconcja\envs
-reticulate\python.exe You can install TensorFlow using the install_tensorflow() function.
Refer to this link:
czcams.com/video/-IYYqdxdYXk/video.html
How did you get to c(10,5)? Is There a rule to find how many hídden layers and how many neurons per hidden layer?
It needs experimentation. You can refer to this:
czcams.com/video/FscOZT0_ObA/video.html
Hi there, How do we inteprete the results of the prediction ? I am not sure I understood it well. Really good video btw. Thanks for sharing your knowledge.
You can refer to this series that has many examples:
czcams.com/video/hd81EH1g1bE/video.html
when I execute this line, m
make sure you run library(keras).
@@bkrai ... i ran keras and am still facing the same issue
Great tutorial Sir. After a long time you have uploaded a new video. Are you busy Sir? I have been waiting for your video for a long a time. Thank you so much for this one.
Thanks for comments! You are right, this one took some time.
How soon we can expect your new video Sir?
Probably in a week or so.
I'll be waiting for your next video Sir. This video took almost a month which is a very long period for all of us who eagerly wait to learn from you. Thank you so much Sir.
It will be quicker this time.
how to prediction future values?thank very much for your response
Future values can be part of data such as test data.
Thanks for the video! Can you show me how to plot the neural net for recognition the digits?
Here is the link:
czcams.com/video/5bso_5X7Zu4/video.html
it is asking to install python package for sequential analysis
Thank you for Nice videoas on Neural Networks. I request for multivariate forecasting using Neural Networks in R. This would be helpful.
Thanks, I've added it to my list.
Respect sir for all that you're doing here it makes a lot of differences.
I'm wondering how I can deploy this exact model on a shiny app?
I've added it to my list, thanks!
Thank you very much Dr. Your videos are amazing as usual. Unfortunately, I can not use this technique to predict the prices of used cars since not all variables are numeric, so Deep NN will not work.
to_categorical() function can be used to convert categorical variables to have integer values.
@@bkrai Ok .. Thank you very much.
thank you sir for the vedio plz i have i question when i create the model in R show me that Python module keras was not found.even that the library of keras works
did you run install_keras()?
@@bkrai yes i do solve the problem by install tensorflow in code it work know but the problem in the evaluation i get big number 1234567 like this i don't know why? And thank you sir
Thanks for the update!
@@bkrai in this case how to fix that to show that the loss increase ? that thank you sir
For that you need to experiement with different number of neurons and different hidden layers.
Dear Sir, Thanks for good video. I am facing following error when, I am trying to excutse "keras_model_sequential()". Error: Installation of Python not found, Python bindings not loaded. Kindly guide me. Thanks
make sure keras is installed.
@@bkrai I installed KERAS but when I execute then it is showing following error
I would suggest start with the 1st video in this playlist:
czcams.com/play/PL34t5iLfZddtC6LqEfalIBhQGSZX77bOn.html
Great Example but every time I try to fit the model I get the following error. I was wondering if you have insight. Again thanks for your amazing well articulated R training!
Epoch 1/200
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: in user code:
Never mind I figured it out! Thanks again for the amazing training!
Thanks for the update!
Great Video. I am getting error in model
Me too
@@jitendraupadhyay6218 You guys need to have Python already installed on your computer. Then you may need restart your R studio and reinstall all required libraries. Or possible even reinstall R studio to make sure all connections are updated.
Thanks for the update!
There are so many elements to tune the model and hence so many ways, how is one supposed to understand which element need to be modified, is it the extra hidden layer or number of neurons or the learning rate?
After some practice with different datasets, you will automatically start getting ideas as to what modification is likely to help.
@@bkrai Thank you sir, I just have one last question and quite basic but I am still confused. In case of quantitative variable error is MSE for neural networks but in case of qualitative variable the data is in 0,1 format ans y_predicted is generally a probability value so what is the error that is calculated and propagated back to neurons through gradient descent for updating weights??
For qualitative variable we can make use of 'accuracy' which is higher the better.
I have a question, how can I set the weight initialization at keras?
It randomizes weights, each time you run model.
@@bkrai Thank you for your answer and your great video. But I have the following question: How can I fix the choice of weights in Keras? Perhaps there is a function like set.Sees () in Keras that. enables this?
I need this to compare which training starting point is better to determine.
do you have convul+lstm tutorial vedio?
You can find it in the book titled "Advanced Deep Learning with R" by me. But I'll certainly do a video in future.
@@bkrai thank you!!! i will look it up!
I wonder why you did not normalize the dependent variable "medv".
No harm in trying that.
what is the purpose of neuralnet package when you have keras loaded?
Just for the plot.
@@bkrai Thank you for clarifying
Thank you very much for this informative video,
How can we use it for classification instead of regression?
Many videos in this playlist are for classification:
czcams.com/play/PL34t5iLfZddtC6LqEfalIBhQGSZX77bOn.html
@@bkrai Thank you very much. Could you make a video for satellite image classification using neural nets?
Send me data that I can look at.
@@bkrai Thanks a lot. Can I send it through email? Could I get a way to send it to you?
seemabharat@gmail.com
I am using rstudio cloud, but for some reason I get an error when I run the last bit of my code i.e Fit Model, would you be able to help where I may be missing it, here is the code and error >>
model = keras_model_sequential()
model %>% layer_dense(units = 5, activation = 'relu', input_shape = c(18)) %>%
layer_dense(units = 1)
#Compile
model %>% compile(loss = 'mse', optimizer = 'rmsprop',
metrics = 'mae')
#Fit model
mymodel = model %>% fit(training, trainingtarget, epochs = 50,
batch_size = 10, validation_split = 0.2)
Error
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: in user code:
/home/rstudio-user/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/home/rstudio-user/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/rstudio-user/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/home/rstudio-user/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/home/rstudio-user/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-pac
Please assist. Thanks
Have you tried this?
czcams.com/video/-IYYqdxdYXk/video.html
@@bkrai thanks much, will look at it.
welcome!
Thanks so much!
It will be great, if you can do autoencoders
Thanks for the suggestion!
Should be the results uncaled? And if so how?
Target is not scaled, so you don't need to unscale.
@@bkrai It's seems my predictions are scaled (pred). Did I forgot something?
Amen
Thanks
When I used this piece of code I am getting this error from your own code and dataset. I didn't change anything. Do you know why? "> model %>%
+ layer_dense(units = 5, activation = 'relu', input_shape = c(13)) %>%
+ layer_dense(units = 1)
Error in eval(lhs, parent, parent) : object 'model' not found" Thank you so much
It says "model" not found. Can you share this as well as previous line of code?
@@bkrai, Ok I am sharing the whole code previous and a preceding line of code after so that you see, Thank you so much for your answer. "# Libraries
library(keras)
library(mlbench)
library(dplyr)
library(magrittr)
library(neuralnet)
# Data
data("BostonHousing")
data
In your original message you had ">" before model. I don't see that in the entire code sent. See below for reference:
Do you know why? "> model %>%
@@bkrai: I was just copying your code and run as you present it. Does it mean that I have to create the "model" somewhere else? I was going to your ode as it's so that I could apply it to my own dataset. I got the code from as it's. Thank you.
Dr. Bharatendra Rai: I think this is my issue" model model
Even when the library(keras) is there I get the same error. Thank you
share codes for model architecture so that I can have a look at it.
Please make a video on Fuzzy logic using R.
Thank you
Thanks, I've added it to my list.
@@bkrai Thank you
Welcome!
Many Thanks Dr Rai, i wanted your guidance to predict 14th Variable for fresh dummy data 'newdata' that i created to see if i can use this for a similar problem. Code attached below, pls help where am i going wrong. Used exactly your code which ran fine. Post which created newdata in same format/class. Below attached is additional code. Getting error: ValueError: No data provided for "dense_2_input". Need data for each key in: ['dense_2_input'] when trying to fit model. Appreciate your support.
val
It says "no data provided". Make sure your data is available.
First of all, thank you! This video is awesome. Everythings works fine except the viewer. I installed the packages tfruns but I dont have a diagram of the fit. I get this message: /session/tfruns-metrics381c56414d7e/index.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A43071 not found
Do anyone know what is wrong and how I can solve the problem? Thank you!
The error message below is what i got when i try to run the model
> model
Probably keras is not installed or running library line was missed.
I am getting this message -Warning message: algorithm did not converge in 1 of 1 repetition(s) within the stepmax.
You can increase it to a higher number.
Hi, I love your videos! Whilst trying to execute keras_model_sequential I get the following error: Error in initialize_python(required_module, use_environment): Installation of Python not found, Python bindings not loaded. I installed R but haven’t installed Python. Please can you advise if there are any easy fixes for this? Thanks 🙃
Make sure you install keras and tensorflow.
Hi, thanks for your reply. I managed to get this working eventually. One thing I feel is missing from this video, is to show how you would get the probability of an outcome using some new data? For example, a file that contains the same data every week. This would make the video complete as it fully walks through the process for creating a model and testing from a sample of data and results using the training/testing partition method, together with how to test on new data, and how to export the results from the new data, into a flat file, for example. I feel your viewers would benefit from this additional content.
Thanks for the suggestion!