deeplearning4j/deeplearning4j · error · java.lang.IllegalArgumentException
Cannot set parameter \"
Error message
Cannot set parameter \"
What it means
SameDiffOutputLayer.setParam throws IllegalArgumentException when the given key does not exist in the layer's parameter table, and when the replacement INDArray's shape differs from the existing parameter's shape. SameDiff-based layers only expose a fixed set of parameters derived from the SameDiff graph, so unknown keys or mismatched shapes are rejected before any mutation. The message includes both the offending key and the current vs attempted shapes.
Solutions
- Print the layer's configured parameter keys via paramTable().keySet() (or conf variables) and use exactly those key names.
- Check the shape of the existing parameter with paramTable.get(key).shape() and reshape/transpose your value INDArray to match before calling setParam.
- If migrating from a non-SameDiff layer, map old parameter names to the SameDiff layer's actual keys instead of reusing 'W'/'b'.
- Verify the layer configuration used to build the network is the same one the saved weights came from.
Example fix
// before
layer.setParam("W", weights);
// after
String key = layer.paramTable().keySet().iterator().next(); // use actual SameDiff param key
if (Arrays.equals(layer.paramTable().get(key).shape(), weights.shape())) {
layer.setParam(key, weights);
} Defensive patterns
Strategy: validation
Validate before calling
if (!layer.paramTable().containsKey(key)) {
throw new IllegalArgumentException("Unknown param key: " + key + "; valid: " + layer.paramTable().keySet());
}
if (!Arrays.equals(layer.paramTable().get(key).shape(), val.shape())) {
throw new IllegalArgumentException("Shape mismatch for key " + key + ": expected "
+ Arrays.toString(layer.paramTable().get(key).shape()) + ", got " + Arrays.toString(val.shape()));
}
layer.setParam(key, val); Type guard
boolean canSetParam(Layer layer, String key, INDArray val) {
return layer.paramTable().containsKey(key)
&& Arrays.equals(layer.paramTable().get(key).shape(), val.shape());
} Try / catch
try {
layer.setParam(key, val);
} catch (IllegalArgumentException e) {
// log available keys + shapes, then fail fast with actionable message
throw new IllegalStateException("setParam failed: " + e.getMessage()
+ "; available keys: " + layer.paramTable().keySet(), e);
} Prevention
- Discover parameter keys via paramTable().keySet() instead of hardcoding 'W'/'b'.
- Always compare Shapes of the existing parameter with the incoming INDArray before setParam.
- When porting weights between layer types, map parameter names explicitly.
- Keep saved weights together with the exact layer configuration that produced them.
When it happens
Trigger: Calling setParam(key, val) or setParamTable(...) with a key that is not one of the layer's registered SameDiff parameter names, or passing an INDArray whose shape() differs from the shape of the existing parameter stored under that key.
Common situations: Copy-network-state code that assumes generic DL4J parameter names (W, b) while SameDiff layers use graph-derived keys; loading weights saved from a differently-configured layer; passing a transposed or batch-appended array.
Understand the failure class
Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.
Related errors
- Expected column vector for mask with 2d input, with same…
- Invalid input, does not match configuration: expected
- Invalid input: expect output columns must be equal to…
- Invalid input: expected output size(1)=
- Invalid input type (layer index = " + layerIndex + ", layer…
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/898d661732c465be.
Report an issue: GitHub.
Appendix: source
Thrown at deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/samediff/SameDiffOutputLayer.java:251
@Override
public INDArray getParam(String param) {
return paramTable.get(param);
}
@Override
public long numParams(){
return params == null ? 0 : (int)params.length();
}
@Override
public void setParam(String key, INDArray val) {
if(!paramTable.containsKey(key)){
throw new IllegalArgumentException("Cannot set parameter, invalid/unknown parameter key: " + key);
}
INDArray current = paramTable.get(key);
if(!Arrays.equals(current.shape(), val.shape())){
throw new IllegalArgumentException("Cannot set parameter \"" + key + "\", invalid shape: parameter array has shape "
+ Arrays.toString(current.shape()) + ", trying to set parameter of shape " + Arrays.toString(val.shape()));
}
}
@Override
public void setParams(INDArray params) {
if (params != null) {
throw new UnsupportedOperationException("Not supported");
}
}
protected void setParams(INDArray params, char order) {
setParams(params);
}
@Override
public void setParamsViewArray(INDArray params) {
this.params = params;View on GitHub (pinned to 4c22ac5fe4)