@Test public void testEmbeddingLayerSetWeightsMaskZero() throws Exception { KerasEmbedding embedding = buildEmbeddingLayer(conf1, keras1, true); embedding.setWeights(Collections.singletonMap(conf1.getLAYER_FIELD_EMBEDDING_WEIGHTS(), Nd4j.ones(INPUT_SHAPE))); INDArray weights = embedding.getWeights().get(DefaultParamInitializer.WEIGHT_KEY); assertEquals(embedding.getWeights().get(DefaultParamInitializer.WEIGHT_KEY).columns(),INPUT_SHAPE[1]); }
@Override public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException { this.weights = new HashMap<>(); if (weights.containsKey("s")) { INDArray kernel = weights.get("s"); weights.remove("s"); weights.put(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS(), kernel); } if (!weights.containsKey(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS())) throw new InvalidKerasConfigurationException( "Parameter " + conf.getLAYER_FIELD_EMBEDDING_WEIGHTS() + " does not exist in weights"); INDArray kernel = weights.get(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); if (this.zeroMasking) { kernel.putRow(0, Nd4j.zeros(kernel.columns())); } this.weights.put(DefaultParamInitializer.WEIGHT_KEY, kernel); if (weights.size() > 2) { Set<String> paramNames = weights.keySet(); paramNames.remove(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); String unknownParamNames = paramNames.toString(); log.warn("Attemping to set weights for unknown parameters: " + unknownParamNames.substring(1, unknownParamNames.length() - 1)); } }
KerasEmbedding extends KerasLayer { @Override public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException { this.weights = new HashMap<>(); if (weights.containsKey("s")) { INDArray kernel = weights.get("s"); weights.remove("s"); weights.put(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS(), kernel); } if (!weights.containsKey(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS())) throw new InvalidKerasConfigurationException( "Parameter " + conf.getLAYER_FIELD_EMBEDDING_WEIGHTS() + " does not exist in weights"); INDArray kernel = weights.get(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); if (this.zeroMasking) { kernel.putRow(0, Nd4j.zeros(kernel.columns())); } this.weights.put(DefaultParamInitializer.WEIGHT_KEY, kernel); if (weights.size() > 2) { Set<String> paramNames = weights.keySet(); paramNames.remove(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); String unknownParamNames = paramNames.toString(); log.warn("Attemping to set weights for unknown parameters: " + unknownParamNames.substring(1, unknownParamNames.length() - 1)); } } }
KerasEmbedding extends KerasLayer { @Override public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException { this.weights = new HashMap<>(); if (weights.containsKey("s")) { INDArray kernel = weights.get("s"); weights.remove("s"); weights.put(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS(), kernel); } if (!weights.containsKey(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS())) throw new InvalidKerasConfigurationException( "Parameter " + conf.getLAYER_FIELD_EMBEDDING_WEIGHTS() + " does not exist in weights"); INDArray kernel = weights.get(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); if (this.zeroMasking) { kernel.putRow(0, Nd4j.zeros(kernel.columns())); } this.weights.put(DefaultParamInitializer.WEIGHT_KEY, kernel); if (weights.size() > 2) { Set<String> paramNames = weights.keySet(); paramNames.remove(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); String unknownParamNames = paramNames.toString(); log.warn("Attemping to set weights for unknown parameters: " + unknownParamNames.substring(1, unknownParamNames.length() - 1)); } } KerasEmbedding();  KerasEmbedding(Map<String, Object> layerConfig);  KerasEmbedding(Map<String, Object> layerConfig, boolean enforceTrainingConfig); }
KerasEmbedding extends KerasLayer { @Override public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException { this.weights = new HashMap<>(); if (weights.containsKey("s")) { INDArray kernel = weights.get("s"); weights.remove("s"); weights.put(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS(), kernel); } if (!weights.containsKey(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS())) throw new InvalidKerasConfigurationException( "Parameter " + conf.getLAYER_FIELD_EMBEDDING_WEIGHTS() + " does not exist in weights"); INDArray kernel = weights.get(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); if (this.zeroMasking) { kernel.putRow(0, Nd4j.zeros(kernel.columns())); } this.weights.put(DefaultParamInitializer.WEIGHT_KEY, kernel); if (weights.size() > 2) { Set<String> paramNames = weights.keySet(); paramNames.remove(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); String unknownParamNames = paramNames.toString(); log.warn("Attemping to set weights for unknown parameters: " + unknownParamNames.substring(1, unknownParamNames.length() - 1)); } } KerasEmbedding();  KerasEmbedding(Map<String, Object> layerConfig);  KerasEmbedding(Map<String, Object> layerConfig, boolean enforceTrainingConfig); EmbeddingSequenceLayer getEmbeddingLayer(); @Override InputType getOutputType(InputType... inputType); @Override int getNumParams(); @Override void setWeights(Map<String, INDArray> weights); }
KerasEmbedding extends KerasLayer { @Override public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException { this.weights = new HashMap<>(); if (weights.containsKey("s")) { INDArray kernel = weights.get("s"); weights.remove("s"); weights.put(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS(), kernel); } if (!weights.containsKey(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS())) throw new InvalidKerasConfigurationException( "Parameter " + conf.getLAYER_FIELD_EMBEDDING_WEIGHTS() + " does not exist in weights"); INDArray kernel = weights.get(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); if (this.zeroMasking) { kernel.putRow(0, Nd4j.zeros(kernel.columns())); } this.weights.put(DefaultParamInitializer.WEIGHT_KEY, kernel); if (weights.size() > 2) { Set<String> paramNames = weights.keySet(); paramNames.remove(conf.getLAYER_FIELD_EMBEDDING_WEIGHTS()); String unknownParamNames = paramNames.toString(); log.warn("Attemping to set weights for unknown parameters: " + unknownParamNames.substring(1, unknownParamNames.length() - 1)); } } KerasEmbedding();  KerasEmbedding(Map<String, Object> layerConfig);  KerasEmbedding(Map<String, Object> layerConfig, boolean enforceTrainingConfig); EmbeddingSequenceLayer getEmbeddingLayer(); @Override InputType getOutputType(InputType... inputType); @Override int getNumParams(); @Override void setWeights(Map<String, INDArray> weights);  }