{"record":{"id":"3fbdbc8eefc9c949","repo":"jax-ml/jax","slug":"can-t-compute-input-and-output-sizes-of-a-len-sha","errorCode":null,"errorMessage":"Can't compute input and output sizes of a {len(shape)}-dimensional weights tensor with default in_axis. Must be at least 2D or specify in_axis explicitly.","messagePattern":"Can't compute input and output sizes of a (.+?)-dimensional weights tensor with default in_axis\\. Must be at least 2D or specify in_axis explicitly\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/nn/initializers.py","lineNumber":227,"sourceCode":"    return random.truncated_normal(\n        key, lower, upper, shape, dtype,\n        out_sharding=out_sharding) * jnp.array(stddev, dtype)\n  return init\n\n@export\ndef _compute_fans(shape: Sequence[int],\n                  in_axis: int | Sequence[int] = -2,\n                  out_axis: int | Sequence[int] = -1,\n                  batch_axis: int | Sequence[int] = ()\n                  ) -> tuple[float, float]:\n  \"\"\"\n  Compute effective input and output sizes for a linear or convolutional layer.\n\n  Axes not in in_axis, out_axis, or batch_axis are assumed to constitute the\n  \"receptive field\" of a convolution (kernel spatial dimensions).\n  \"\"\"\n  if isinstance(in_axis, int) and in_axis == -2 and len(shape) <= 1:\n    raise ValueError(\n        f\"Can't compute input and output sizes of a {len(shape)}-dimensional\"\n        \" weights tensor with default in_axis. Must be at least 2D or specify\"\n        \" in_axis explicitly.\"\n    )\n\n  if isinstance(in_axis, int):\n    in_size = shape[in_axis]\n  else:\n    in_size = math.prod([shape[i] for i in in_axis])\n  if isinstance(out_axis, int):\n    out_size = shape[out_axis]\n  else:\n    out_size = math.prod([shape[i] for i in out_axis])\n  if isinstance(batch_axis, int):\n    batch_size = shape[batch_axis]\n  else:\n    batch_size = math.prod([shape[i] for i in batch_axis])\n  receptive_field_size = math.prod(shape) / in_size / out_size / batch_size","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/nn/initializers.py#L209-L245","documentation":"Variance-scaling initializers (variance_scaling, glorot_*, lecun_*) call _compute_fans to derive fan_in/fan_out. With the default in_axis=-2 they need at least a 2D shape; a 0D or 1D shape cannot yield a fan, so this ValueError is raised.","triggerScenarios":"Calling jax.nn.initializers.glorot_uniform()(key, (n,)) or variance_scaling(...)(key, (3,)) — a single-dimension shape — with default in_axis.","commonSituations":"Initializing 1D bias-like vectors or scalar parameters with a fan-based initializer; passing a flattened shape accidentally; layer code where the weight shape is computed wrong.","solutions":["Use at least a 2D shape, e.g. (n, 1) or the true (fan_in, fan_out) matrix shape","For 1D parameters use delta_initializer, zeros, or normal/uniform initializers instead","Alternatively pass in_axis explicitly (e.g. in_axis=0) to disambiguate the fan computation"],"exampleFix":"// before\nw = jax.nn.initializers.lecun_normal()(key, (256,))\n\n// after\nw = jax.nn.initializers.lecun_normal()(key, (256, 1))\n# or for vectors:\nb = jax.nn.initializers.normal()(key, (256,))","handlingStrategy":"validation","validationCode":"def fan_init(init_fn, key, shape):\n    if len(shape) < 2:\n        raise ValueError('fan-based initializers need >=2D shape')\n    return init_fn(key, shape)","typeGuard":"def is_fan_shape(shape) -> bool: return len(shape) >= 2","tryCatchPattern":null,"preventionTips":["Route 1D params to zeros/normal initializers in layer code","Add a shape-policy check in generic parameter-init factories"],"tags":["jax","nn","initializers","shape-validation"],"backgroundTag":"rank-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}