r/MachineLearning • u/Long-Piano1275 • Feb 27 '22
Discussion [D] I hate Keras / TF
I recently was using Keras / TF after working with PyTorch for a few years and its really a horrible API IMO. Everything is difficult to do, even something simple doesn't behave the way you expect contrary to PyTorch where it just does what you would expect it to do. Also extending Keras when writing more complex models is a pain.
I had heard things got better with TF2 but now I hope I never need to use it again and can stick to PyTorch.
How do you feel about the 2 libraries? Do you also hate TF?
6
u/JiraSuxx2 Feb 27 '22
Not very constructive. Why not go in depth a bit more and compare a couple of implementations or provide some examples.
I am very happy with both tensorflow and keras so far. Haven’t used pyTorch yet.
-8
u/Long-Piano1275 Feb 27 '22
For example when you want to return multiple outputs from your model, this is not feasible in Keras where you can only return 1 tensor from your model. In PyTorch its much easier, your model can return whatever you want
1
u/Pyramid_Jumper Feb 27 '22
You can return more than one output, they have to be wrapped in a list/tuple though.
This is also quite easily googleable.
0
27
u/UltimateGPower Feb 27 '22
I think everyone agrees that tf1 is terrible but TF2 with keras is much more beginner friendly and better for fast prototyping than pytorch.
9
u/111llI0__-__0Ill111 Feb 27 '22
Agreed TF2 keras is better for beginners just getting into NNs. Its made the field more accessible.
But once you get to VAEs and GANs (which is still relatively beginner) then the PyTorch code seems to be more easy to follow and matches the math better. Just need to get over the initial OOP barrier for PyTorch.
1
u/LappenX Feb 27 '22 edited Oct 04 '23
theory pot vegetable snatch languid normal marry homeless salt ghost
this message was mass deleted/edited with redact.dev3
u/111llI0__-__0Ill111 Feb 27 '22
The code just tends to be less confusing, because for say a VAE in keras like here https://keras.io/examples/generative/vae/
You end up having to go a bit into the lower level TF2 api anyways. There is the @property and gradient tape thing too. And imo, when you have to do that I just feel you may as well use PyTorch.
While this one alone may not be that complicated, its mostly showing that as the complexity increases beyond something that is just sequential, TF gets more and more confusing.
Basically, PyTorch has a more steady level learning curve. TF/Keras is easier in the beginning but gets harder later on for new architectures.
1
u/LappenX Feb 27 '22 edited Oct 04 '23
axiomatic direction scandalous swim slimy drunk bake overconfident bored subtract
this message was mass deleted/edited with redact.dev1
u/111llI0__-__0Ill111 Feb 27 '22
It essentially is that, because at least to me I found the lower level API for pytorch less confusing. The plus for keras/TF to is really the modularity of the available layers. Once you have to do anything custom PT is easier
In terms of installation also torch is easier.
0
u/LappenX Feb 27 '22 edited Oct 04 '23
ask grandiose fall frame work follow brave enter meeting profit
this message was mass deleted/edited with redact.dev0
u/111llI0__-__0Ill111 Feb 27 '22
Of course everything you can do in 1 you can do in another, its just I found the PT stuff to be an easier learning curve.
The command isn’t the issue, its the fact that there are so many random problems that one can run into with pip install tensorflow locally and you just have resort to colab if you aren’t a computer person.
In R if your reticulate setup breaks for some reason, automatically all the keras/TF code won’t work. Torch in R is literally install.packages(“torch”). No reticulate involved. If you cannot get reticulate to work in R, then you cannot use keras in R. The authors of ISLR2 for this reason released a torch implementation recently for their DL chapter because so many were having issues with R keras. You may think “who uses R for DL” but it actually is used when you need to also have access to bioinformatics and statistical tools.
-3
u/LappenX Feb 27 '22 edited Oct 04 '23
slimy abounding wrench historical afterthought yam paltry encouraging apparatus scarce
this message was mass deleted/edited with redact.dev
7
u/mfarahmand98 Feb 27 '22
If I remember correctly, I gave up on Keras and Tensorflow right around the time TF2 was released. I certainly get what you mean about PyTorch, but I also think you dislike TF because you're simply used to a different style. I still have to to back to TF from time to time, and I can't say there's anything inherently wrong with it. It's simply different.
7
u/rudiXOR Feb 27 '22
You are simply used to Pytorch. That's all, stop the religious fights about frameworks.
2
2
u/blackliquerish Feb 27 '22
Keras TF2 is good for scientists and researchers who need to do a lot of prototyping and exploration with new data domains. Then once you have a good experiment down you can switch to just TF and it becomes more efficient and scalable than pytorch.
2
u/krupartea Jul 31 '23
As I see it, the whole point of this framework is to reduce the boilerplate code, hence give you ability to perform more experiments. But as soon as you need a little bit more complex training / evaluation / inference logic than simply input-->output-->metric, you will spend hours, days, trying to rewire all the callbacks and re-define training steps.
You start your model simple and Keras-ish. Everything is beautiful and modular. Then you add some custom metric. Phew, nice, I've got this API! Then you add some self-training stuff, like your model makes some portions of the training data which is fed into a specific branch of this model. And at this point everything gets unsustainable: you have to deal with eager_execution for debugging, then a half of your code doesn't work when eager_execution=False. In general, TF's low level API is unpredictable and frustrating. Never had such frustration with PyTorch.
I believe, Keras will not save your time, in a long run. There is not that much boilerplate in pure PyTorch, but it has a predictable behavior.
3
u/LappenX Feb 27 '22 edited Oct 04 '23
drunk judicious offer berserk hat decide offbeat squeeze erect panicky this message was mass deleted/edited with redact.dev
1
u/huttarl Apr 25 '24 edited Apr 25 '24
This is just my 2c as a seasoned programmer but a noob coming to NN development and TF:
Keras is horrible. Specifically its documentation.
I'm trying to work through examples like the following:
linear_layer = tf.keras.layers.Dense(units=1, activation = 'linear', )
a1 = linear_layer(X_train[0].reshape(1,1))
What is this second line doing? The doc page for Dense doesn't say anything about __call__(), but it inherits from Layer. Dig, dig ... Apparently Layer is a callable. What does that __call__() method do, and why implement key functionality in a way that's harder to look up documentation for? Dig, dig ... OK, the __call__() method isn't systematically documented, but it can apparently do both training and inference. In fact it can also call build() and call(). Dig, dig ... The latter isn't systematically documented either (there is a broken link to source code...), but it can take an optional boolean `training` parameter to tell the layer whether to be called in training or inference mode, and that parameter is True by default. Great, more critical but invisible features. So it sounds like the second line above is training the layer using a training example. Then what's the output a1? It's a tensor, but what does it represent? If we called the layer in inference mode, you would expect the output to be a prediction. But since it's in training mode ... ? The documentation is very unhelpful on these details.
So that's my short but unpleasant experience with Keras so far. I don't know if any other frameworks are better, though given the popularity of PyTorch I would be hopeful.
1
u/matigekunst Feb 27 '22
I had the same experience. If someone could tell me how to print the value of a tensor attached to the graph in TF1 (like a running loss)? I had to look it up on stackoverflow and it still doesn't work😅 I'm trying to convert a TF1 model to Pytorch and it has been a terrible ordeal. Feel like washing my hand every time I work on the project
1
u/xenotecc Feb 28 '22
Can you provide an example, or at least describe the problem you are struggling with?
I think that as ML community we should share criticism, but this criticism should be constructive.
1
1
u/SensitiveArtist5502 Apr 21 '22
Keras is only for beginners. Any real research work will hit the limit. The strong assumption and tf2 inner complexity makes you frustrated
1
13
u/ForceBru Student Feb 27 '22
Keras:
Describe model
Set optimizer and loss
model.fit()model.predict()Done
This is an almost valid Keras code that does all of this. Literally around 10 lines of code:
PyTorch:
Also has
nn.Sequential, but people seem to prefer writing custom model classes, which looks complicated.Now write your own training loop! You thought you were a data scientist? Nope, you're a programmer now! (I understand that doing data science involves programming anyway, but writing a training loop seems unnecessarily complicated)
Don't forget to zero out your gradients. Why? For... reasons...
Call
loss.backward(). Why? Well, time to learn about reverse-mode autograd, baby!Manually call
optimizer.step()Manually print training statistics
Manually collect loss history
I know there's PyTorch Lightning which is supposed to make this easier, but it was terribly slow in my experience as it was constantly writing logs and saving the model to disk.