Help me debug this: proj = curlsavector @ givenspace @ givenspace.T ValueError: matmul:…
- User
Help me debug this:
proj = cur_lsa_vector @ given_space @ given_space.TValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 39)
The original code:
def new_semantic_content(self, time: int) -> float: """ Newness of contribution at time t. Calculate the newness of the contribution at a given time point: n(c_t) = ||new_t|| / (||new_t|| + ||given_t||) First, calculates the span of contributions up to time t, G: basis candidates, shape (dim, r) or (r, dim) V: contributions, shape (n_samples, dim) Then, computes the projection and residual, each shape (n_samples, dim) which are equivalent to eq. 22 and 23 in the paper """ given_space = self.span_given(time) cur_lsa_vector = self.get_lsa_vector(time).reshape(1, -1) if given_space.size == 0: return 1.0 # all new if no prior contributions # projection of each row vector in v: proj = v @ Q @ Q.T proj = cur_lsa_vector @ given_space @ given_space.T - Assistant
I see what's happening. The error vbnetCopyEditValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 39)
means that in one of your matrix multiplications, the dimensions don’t line up for matrix multiplication.
Likely shapes in your code
cur_lsa_vector: After reshape(1, -1), this has shape (1, dim).
given_space: From self.span_given(time), this is probably (dim, r) or (r, dim) depending on implementation.
Your line: pythonCopyEditproj = cur_lsa_vector @ given_space @ given_space.T
works only if:
cur_lsa_vector is (1, dim)
given_space is (dim, r)
because: rCopyEdit(1, dim) @ (dim, r) = (1, r) (1, r) @ (r, dim) = (1, dim)
But the error suggests given_space might instead be (r, dim) or even (1, 39), causing a mismatch.
How to fix it
Check given_space shape:
- User
print("cur_lsa_vector:", cur_lsa_vector.shape) print("given_space:", given_space.shape)
cur_lsa_vector: (1, 39) given_space: (100, 0)
I think I need help with the function span_given
- Assistant
Ah, now it makes sense:
cur_lsa_vector is (1, 39), so each contribution is a 39-dimensional vector.
given_space is (100, 0), meaning it has 100 rows but zero columns — so it's basically empty, which explains why the projection fails when you try to multiply with it.
That means your span_given function is returning an empty matrix instead of a basis spanning previous contributions.
Likely issue with span_given It sounds like span_given(time) is supposed to collect all previous contributions up to time and return a matrix whose columns form a basis of that space. So if you have previous vectors v1,v2,…,vkv_1, v_2, \ldots, v_kv1,v2,…,vk, each of dimension dim=39\text{dim}=39dim=39, given_space should be something like (dim, k) (39 × k). Instead, you’re getting (100, 0) — so maybe you’re stacking wrong, or taking rows instead of columns.
- User
Whats wrong here?
def span_given(self, time: int) -> np.ndarray: """ Return orthonormal basis of contributions up to time t. Return an orthonormal basis Q for G_t (columns are orthonormal basis vectors), call get_embedding for each contribution up to time t. """ # rows before contribution_id == time idxs = list(range(time - 1)) # because time-1 rows are before current if not idxs: return np.empty((self.n_components, 0)) lsa_matrix = self.lsa_vectors[idxs].T # shape (dim, m) # transpose to (dim, m) then orthonormalize columns return orth(lsa_matrix.T) # returns (dim, k) with orthonormal columns - Assistant
I see the problem now — the way you’re transposing things in span_given is flipping dimensions incorrectly, so you end up with (100, 0) instead of (dim, k). Let’s break it down:
Current code: pythonCopyEditlsa_matrix = self.lsa_vectors[idxs].T # shape (dim, m) return orth(lsa_matrix.T) # returns (dim, k)
self.lsa_vectors[idxs] gives you a subset of your LSA vectors. If self.lsa_vectors has shape (n_contribs, dim), then
self.lsa_vectors[idxs] → (len(idxs), dim) = (m, dim)
After .T → (dim, m)
Then you call .T again before orth, so you give orth an array of shape (m, dim). That means orth gives you (m, k), not (dim, k).
Correct version You want your basis with columns = orthonormal basis vectors in dimension dim. So you should not double-transpose. Just pass the (dim, m) matrix directly to orth: pythonCopyEditdef span_given(self, time: int) -> np.ndarray: idxs = list(range(time - 1)) if not idxs: return np.empty((self.n_components, 0))
- User
still print("cur_lsa_vector:", cur_lsa_vector.shape) print("given_space:", given_space.shape) cur_lsa_vector: (1, 39) given_space: (100, 0)
- Assistant
Got it — if you’re still getting (100, 0), it means the shape problem is happening before the projection math. Specifically, your span_given is returning a matrix with self.n_components = 100, but your vectors have dimension 39, so the basis dimension is wrong. Let’s step through carefully:
What you likely have
self.lsa_vectors: shape (n_samples, dim) = (time, 39)
self.n_components: you set this somewhere, maybe to 100
But your LSA vectors are only 39-dimensional, so any basis for them should have shape (39, k), never (100, k). That’s why np.empty((self.n_components, 0)) gives (100, 0).
Fix span_given Here’s a corrected version that adapts automatically to the actual dimension: pythonCopyEditdef span_given(self, time: int) -> np.ndarray: """ Return orthonormal basis of contributions up to time t. """ idxs = list(range(time - 1)) # contributions before current if not idxs: # Use actual vector dimension, not n_components dim = self.lsa_vectors.shape[1] return np.empty((dim, 0))
- User
I'm always getting e^-16 for the newness score
- Assistant
Ah — if your newness score is always on the order of e−16e^{-16}e−16, that usually means the projection residual is nearly zero compared to the projected vector, so n(ct)=∣∣newt∣∣∣∣newt∣∣+∣∣givent∣∣n(c_t) = \frac{||\text{new}_t||}{||\text{new}_t|| + ||\text{given}_t||}n(ct)=∣∣newt∣∣+∣∣givent∣∣∣∣newt∣∣ is tiny because ∣∣newt∣∣≪∣∣givent∣∣||\text{new}_t|| \ll ||\text{given}_t||∣∣newt∣∣≪∣∣givent∣∣. But since you just fixed span_given, I suspect the problem isn’t the math itself but rather:
given_space might always span everything → so projection captures the full vector, leaving no residual.
- User
help me implement the given space. Its always almost 1
- Assistant
Ah, I get it now: your given space is growing so fast (or in the wrong way) that it quickly spans everything, so the residual goes to nearly zero → your newness score goes to almost 1. What you want instead is:
A basis QQQ that only contains linearly independent directions from previous contributions, so redundant vectors don’t blow up the space.
Orthonormalization so projection math is stable.
The simplest fix is to build QQQ using QR decomposition or SVD on all previous vectors up to time ttt.
Clean implementation for span_given This will keep only independent directions and give you a proper basis: pythonCopyEditimport numpy as np
- User
explain this np.linalg.qr(lsa_matrix.T)
- Assistant
Sure — let’s break it down clearly.
- What lsa_matrix.T is
Suppose lsa_matrix has shape (m, dim) → m previous contributions, each of dimension dim.
Transposing gives you (dim, m) so each column is one contribution vector in ℝ^dim.
That’s exactly what we want: a matrix V∈Rdim×mV \in \mathbb{R}^{\text{dim} \times m}V∈Rdim×m where columns = vectors.
- What np.linalg.qr() does When you call pythonCopyEditQ, R = np.linalg.qr(V)
on a matrix VVV, it factors it into V=QRV = Q RV=QR where
QQQ has orthonormal columns (basis vectors),
RRR is upper triangular (not important for us).
So if VVV is dim×m\text{dim} \times mdim×m, then
QQQ is dim×k\text{dim} \times kdim×k where k=min(dim,m)k = \min(\text{dim}, m)k=min(dim,m)
Columns of QQQ span the same space as the original vectors but are orthonormal and linearly independent.
- User
The given space is still almost always 1
- Assistant
Ah, so now the given space QQQ is still covering almost everything, which makes the projection norm ∥proj∥|\text{proj}|∥proj∥ dominate and the newness score n(ct)=∥resid∥∥resid∥+∥proj∥n(c_t) = \frac{|\text{resid}|}{|\text{resid}| + |\text{proj}|}n(ct)=∥resid∥+∥proj∥∥resid∥ nearly zero every time. That means your basis is expanding so quickly that after a few contributions it spans almost the entire space. In a 39-dimensional space, after 39 independent contributions the span is full rank, so any new vector will be nearly fully projected, leaving almost no residual.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.