-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ML keras #4172
base: master
Are you sure you want to change the base?
ML keras #4172
Conversation
jenkins build this please |
jenkins build this please |
This does not add a dependency on a third party library, so then I assume it instead embeds Keras in some way? Or have I misunderstood the purpose of this PR? |
Is there a reason this is added to opm-common instead of being a separate repository? Do we, somehow, need to make (selected) objects in this repository, or any of its downstream repositories for that matter, "aware" of Keras? |
We use Keras for the training process (it doesnt need to be done in OPM). The generated models are subsequently embedded and run in OPM. I have updated the description to provide some context. |
jenkins build this please |
jenkins build this please |
Maybe I'm missing something, but as far as I can tell no-one have answered my question from last week
I would really like an answer to this before I consider the details of the PR. |
Sorry for not answering earlier. The idea is to apply the ML-Keras inside OPM for different tasks. This is only the first PR to add the Keras ML model. The applications will follow. For an example of a ML near well model using ML-Keras check out https://github.com/cssr-tools/ML_near_well. Since the ML-Keras model framework is general. We hope it would be useful for the OPM community and therefore suggest to add it to opm-common |
Okay, utility/convenience is clearly one reason for adding it here. Would it be impossible to make [your/certain use cases] work if it were located elsewhere? Do you, for instance, need access to the internals/private data members or member functions of |
jenkins build this please |
Exact! For instance, we need access to the automatic differentiation tools within OPM. |
jenkins build this please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a lot of changes needed here. I have only looked at the C++ code, and I probably missed some. I have not really checked that the activation functions or the layers do what a user of Keras would expect. I have not looked at any of the Python code, someone else must do that.
I have requested many changes, but I hope it provides a useful learning experience. Feel free to ask about anything that is unclear!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments on the python bits. I think a major point is that the folder opm/ml_tools
, which contains only python code, should probably be moved to say the python
folder or similar. And since these scripts use external libraries (tf, numpy, keras) I would really like to see a requirements.txt
file specifying the versions used. Especially tensorflow is known to be problematic her.
Thanks all for the valuables comments and suggestions. |
jenkins build this please |
1 similar comment
jenkins build this please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have looked at the c++ parts, which have been improved a lot. Still quite a few things to address, but I think this will converge eventually!
opm/ml/keras_model.hpp
Outdated
data_.resize(i * j * k * l); | ||
} | ||
|
||
inline void Flatten() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still a problem.
opm/ml/ml_model.hpp
Outdated
|
||
|
||
template <typename Type> | ||
void resizeI(std::vector<Type> c) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason to take a copy of c
, use a const reference. Also, you do not need to force this to be a vector:
template <typename Sizes>
void resizeI(const Sizes& sizes)
{
...
}
opm/ml/ml_model.cpp
Outdated
template<class Evaluation> | ||
bool NNLayerScaling<Evaluation>::loadLayer(std::ifstream& file) { | ||
OPM_ERROR_IF(!readFile<float>(file, data_min), "Failed to read min"); | ||
OPM_ERROR_IF(!readFile<float>(file, data_max), "Failed to read min"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error messages are wrong, except for the first.
opm/ml/ml_model.cpp
Outdated
break; | ||
case kHardSigmoid: | ||
for (size_t i = 0; i < out.data_.size(); i++) { | ||
Evaluation x = (out.data_[i] * sigmoid_scale) + 0.5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be const
.
opm/ml/ml_model.cpp
Outdated
break; | ||
case kSigmoid: | ||
for (size_t i = 0; i < out.data_.size(); i++) { | ||
Evaluation& x = out.data_[i]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be const
. Always make variables const unless they have to be mutable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This review block just contains a single comment, from a long time ago, but that is still relevant. I will make a new review block now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from reformatting, I see none of the earlier requested code changes here.
opm/ml/ml_model.hpp
Outdated
*/ | ||
template <class T> class Tensor { | ||
public: | ||
Tensor() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation and formatting is not in line with common OPM practice, although it seems internally consistent now! I assume you used clang-format to process this? Please do so again, using the .clang-format file at the top level of opm-common. (That would give 4-space indents for example.)
You could consider setting a slightly wider allowed width to make the fmt::format() calls look nicer (i.e. on a single line) such as on line 78.
opm/ml/ml_model.hpp
Outdated
std::accumulate(begin(c), end(c), 1.0, std::multiplies<Type>())); | ||
} | ||
|
||
inline void flatten() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove inline
in front of functions that are defined inline in the class. It is unnecessary.
This was commented on already!
opm/ml/ml_model.hpp
Outdated
|
||
Tensor(int i, int j, int k, int l) { resizeI<int>({i, j, k, l}); } | ||
|
||
template <typename Type> void resizeI(std::vector<Type> c) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See earlier comment on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I recall correctly, you asked to use the dims_, not c?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the comment:
No reason to take a copy of c, use a const reference. Also, you do not need to force this to be a vector:
template <typename Sizes>
void resizeI(const Sizes& sizes)
{
...
}
The use of dims_ was further down in an accumulate call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use now a const ref.
opm/ml/ml_model.hpp
Outdated
"Cannot add tensors with different dimensions"); | ||
Tensor result; | ||
result.dims_ = dims_; | ||
result.data_.reserve(data_.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a resize(), and then not use a back_inserter below, as discussed earlier! Same with the multiply() further down.
opm/ml/ml_model.hpp
Outdated
template <class Evaluation> | ||
class NNLayerActivation : public NNLayer<Evaluation> { | ||
public: | ||
enum ActivationType { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was requested to make this an enum class, and you commented that this was done, but it is not done.
opm/ml/ml_model.cpp
Outdated
template <class Evaluation> | ||
bool NNLayerActivation<Evaluation>::apply(const Tensor<Evaluation>& in, | ||
Tensor<Evaluation>& out) { | ||
constexpr double sigmoid_scale = 0.2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not define this up here, but just before it is used (line 96). As already commented...
Note that in addition to re-commenting on several of the unchanged problems, I un-resolved several more since the existing comment was still valid. Please make code changes first, in a separate commit so I can easily see what changes you made, then after that add a separate commit to do the auto-reformatting. |
Thanks for spotting the missing points. The changes were done in November. However, unfortunately, during re-basing we removed some commits inadvertently. The requested changes will all be added back. |
Integrating Keras capabilities to OPM.
Draft pull request to test/discuss implications of the changes in OPM.
This enables the straightforward and adaptable integration of neural networks into OPM scripts. These models are initially trained using the Keras library in Python, stored in a format readable for the OPM framework and subsequently deployed within OPM.
When the user initializes and loads a stored Keras model inside an OPM script, an automated deployment process handles all the translation. This process works by operating a series of steps handling model interpretation, layer conversion, optimization, and code generation steps to adapt the Keras model to a native OPM function.