Skip to content
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

Keras examples for single input and multiple inputs have been added. #286

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

justnoxx
Copy link

These examples are explaining how to use saved models created using Keras.
1st example explains how to use the model when there is precisely one input operation.
2nd example shows how to deal with models where more than one input ops.

@google-cla
Copy link

google-cla bot commented Dec 18, 2020

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@justnoxx
Copy link
Author

@googlebot I signed it!

@justnoxx
Copy link
Author

@adamcrume there are some examples related to Keras that I mentioned in #282
Please, let me know what I should change if required to make it merged.
Thanks.

/Dmitriy

Copy link
Contributor

@adamcrume adamcrume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a few minor style issues.

Also, please run cargo fmt.

let save_dir = "examples/keras_multiple_inputs_saved_model";
let v1: Vec<f32> = vec![0.1, 0.2, 0.3, 0.4, 0.5];
let v2 = vec![0.6, 0.7, 0.8, 0.9, 0.1];
let tensor1 = vector_to_tensor(&v1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified to:

let tensor1 = Tensor::from(&[0.1, 0.2, 0.3, 0.4, 0.5][..]);

no Vec or vector_to_tensor needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function is removed.

// In this file test_in_input is being used while in the python script,
// that generates the saved model from Keras model it has a name "test_in".
// For multiple inputs _input is not being appended to the op name.
let input_1_op_name = "test_in1";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't op names, they're signature input (or output) parameter names. I know this sounds like nitpicking, but this can be confusing, so it's important to make a clear distinction, so readers don't think they're the same thing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable is renamed now.

let result = session.run(&mut args);
if result.is_err() {
panic!("Error occured during calculations: {:?}", result);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified to:

session.run(&mut args).expect("Error occurred during calculations");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplification has been performed.

print(x1.shape)
y = np.random.randn(100, 1)
# print(v1.shape)
# print(v2.shape)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were these commented-out lines intended to be removed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete lines have been removed.

let dimension = v.len();
let tensor = Tensor::new(&[1, dimension as u64]).with_values(&v[..]).unwrap();
return tensor;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file name has a typo: signle should be single.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File has been renamed

let save_dir = "examples/keras_signle_input_saved_model";
let v: Vec<f32> = vec![0.1, 0.2, 0.3, 0.4, 0.5];

let tensor = vector_to_tensor(&v);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with the other file, this can be simplified to:

let tensor = Tensor::from(&[0.1, 0.2, 0.3, 0.4, 0.5][..]);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it did not work for me because your code is creating tensor of dims:[5], but dim:[1, 5] is expected, so I used the following code:

let tensor: Tensor<f32> = Tensor::new(&[1, 5])
        .with_values(&[0.1, 0.2, 0.3, 0.4, 0.5])
        .expect("Can't create tensor");

// In this file test_in_input is being used while in the python script,
// that generates the saved model from Keras model it has a name "test_in".
// For multiple inputs _input is not being appended to the op name.
let input_op_name = "test_in_input";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't op names.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable has been renamed.

let result = session.run(&mut args);
if result.is_err() {
panic!("Error occured during calculations: {:?}", result);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can use expect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expect has been added.

@justnoxx
Copy link
Author

@adamcrume sorry for delay, literally did not have time to address that, but now I did the changes that you requested. A file with typo in its name has been renamed. Cargo fmt has been also applied. Please, let me know if I need to do additional updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants