keras

Use layer output in keras custom loss

≡放荡痞女 提交于 2021-02-09 02:48:52
问题 I am developing a custom loss function in Keras and I need the first layer output. How can I retrieve it? def custom_loss(y_true, y_pred): cross = K.mean(K.binary_crossentropy(y_true, y_pred), axis = 1) layer_output = model.get_layer_output(1) # this is what i'd like to use return cross + perturb 回答1: Checking the docs you can retrieve a layer by using the model.get_layer() method. You can then pass the desired index or well pass the name of the layer. After getting a layer you can easily

Use layer output in keras custom loss

那年仲夏 提交于 2021-02-09 02:47:33
问题 I am developing a custom loss function in Keras and I need the first layer output. How can I retrieve it? def custom_loss(y_true, y_pred): cross = K.mean(K.binary_crossentropy(y_true, y_pred), axis = 1) layer_output = model.get_layer_output(1) # this is what i'd like to use return cross + perturb 回答1: Checking the docs you can retrieve a layer by using the model.get_layer() method. You can then pass the desired index or well pass the name of the layer. After getting a layer you can easily

How Keras IMDB dataset data is preprocessed?

不羁的心 提交于 2021-02-08 20:01:28
问题 I'm working on a problem of sentiment analysis and have a dataset, which is very similar to Kears imdb dataset. When I load Keras’s imdb dataset, it returned sequence of word index. (X_train, y_train), (X_test, y_test) = imdb.load_data() X_train[0] [1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941, 4, 173, 36, 256, 5, 25, 100, 43, 838, 112, 50, 670, 22665, 9, 35, 480, 284, 5, 150, 4, 172, 112, 167, 21631, 336, 385, 39, 4, 172, 4536, 1111, 17, 546, 38, 13, 447, 4, 192, 50, 16,

Load model with ML.NET saved with keras

倖福魔咒の 提交于 2021-02-08 19:57:19
问题 I have a Neural Network implemented in Python with Keras. Once I have trained it I have exported the model and I have got two files: model.js and model.h5. Now I want to classify in real time inside a .NET project and I want to use the trained Neural Network for it. Is there a way in ML.NET of loading the model and trained weights exported with python into a model object? I have seen in the documentation[1] that a previous saved model can be loaded, but apparently is storage in a .zip and I

Can not squeeze dim[1], expected a dimension of 1, got 499

别来无恙 提交于 2021-02-08 17:24:25
问题 I am trying to make an AutoEncoder and am stuck at the above error. Looking at other posts with this on Stack Exchange didn't help. Here is the error in full: InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 499 [[{{node metrics_12/acc/Squeeze}}]] [[{{node ConstantFoldingCtrl/loss_12/time_distributed_6_loss/broadcast_weights/assert_broadcastable/AssertGuard/Switch_0}}]] I can compile my model. Here it is: Layer (type) Output Shape Param # ==========================

Can not squeeze dim[1], expected a dimension of 1, got 499

半腔热情 提交于 2021-02-08 17:23:12
问题 I am trying to make an AutoEncoder and am stuck at the above error. Looking at other posts with this on Stack Exchange didn't help. Here is the error in full: InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 499 [[{{node metrics_12/acc/Squeeze}}]] [[{{node ConstantFoldingCtrl/loss_12/time_distributed_6_loss/broadcast_weights/assert_broadcastable/AssertGuard/Switch_0}}]] I can compile my model. Here it is: Layer (type) Output Shape Param # ==========================

Tensorflow 2.0: How can I fully customize a Tensorflow training loop like I can with PyTorch?

假装没事ソ 提交于 2021-02-08 11:42:17
问题 I used to use Tensorflow a lot before, but moved over to Pytorch because it was just a lot easier to debug. The nice thing I found with PyTorch is that I have to write my own training loop, so I can step through the code and find errors. I can fire up pdb and check the tensor shapes and transformations, etc., without difficulty. In Tensorflow I was using the model.fit() function all the time, and so any error message I got was like 6 pages of C code where the error message did not give me any

TypeError when trying to predict labels with argmax

落花浮王杯 提交于 2021-02-08 11:20:25
问题 I have successfully followed this transfer learning tutorial to make my own classifier with two classes, "impressionism" and "modernism". Now trying to get a label for my test image, applying advice from this thread: y_prob = model.predict(new_image) y_prob (gives this output) array([[3.1922062e-04, 9.9968076e-01]], dtype=float32) y_classes = y_prob.argmax(axis=-1) y_classes (gives this output) array([1]) # create a list containing the class labels labels = ['modernism', 'impressionism']

Finetuning VGG-16 on GPU in Keras: memory consumption

血红的双手。 提交于 2021-02-08 11:20:20
问题 I'm fine-tuning VGG-16 for my task. The idea is that I load the pretrained weights, remove the last layer (which is softmax with 1000 outputs) and replace it with a softmax with a few outputs. Then I freeze all the layers but the last and train the model. Here is the code that builds the original model and loads the weights. def VGG_16(weights_path=None): model = Sequential() model.add(ZeroPadding2D((1,1),input_shape=(224,224,3))) model.add(Conv2D(64, (3, 3), activation='relu')) model.add

Keras hyperparameter autotune shortening the path

被刻印的时光 ゝ 提交于 2021-02-08 11:12:58
问题 this is the code that I am running. It is a self hyperparameter tuned ANN. The path is to long and I don't know how to shorten it. def build_model(hp): model = keras.Sequential() for i in range(hp.Int('num_layers', 2, 20)): model.add(layers.Dense(units=hp.Int('units_' + str(i), min_value=32, max_value=512, step=32), activation='relu')) model.add(layers.Dense(1, activation='linear')) model.compile( optimizer=keras.optimizers.Adam( hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])), loss='mean