Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fig = plt.figure()
for i, pattern in enumerate(patterns):
print('fitting pattern n.{}'.format(i))
# Generate the original image
weight_img = gen_image(region=pattern, image_height=image_height, image_width=image_width)
weight_img = tl.tensor(weight_img)
# Generate the labels
y = tl.dot(partial_tensor_to_vec(X, skip_begin=1), tensor_to_vec(weight_img))
# Plot the original weights
ax = fig.add_subplot(n_rows, n_columns, i*n_columns + 1)
ax.imshow(tl.to_numpy(weight_img), cmap=plt.cm.OrRd, interpolation='nearest')
ax.set_axis_off()
if i == 0:
ax.set_title('Original\nweights')
for j, rank in enumerate(ranks):
print('fitting for rank = {}'.format(rank))
# Create a tensor Regressor estimator
estimator = TuckerRegressor(weight_ranks=[rank, rank], tol=10e-7, n_iter_max=100, reg_W=1, verbose=0)
# Fit the estimator to the data
estimator.fit(X, y)
ax = fig.add_subplot(n_rows, n_columns, i*n_columns + j + 2)
ax.imshow(tl.to_numpy(estimator.weight_tensor_), cmap=plt.cm.OrRd, interpolation='nearest')
ax.set_axis_off()
ax = fig.add_subplot(n_rows, n_columns, i*n_columns + 1)
ax.imshow(tl.to_numpy(weight_img), cmap=plt.cm.OrRd, interpolation='nearest')
ax.set_axis_off()
if i == 0:
ax.set_title('Original\nweights')
for j, rank in enumerate(ranks):
# Create a tensor Regressor estimator
estimator = KruskalRegressor(weight_rank=rank, tol=10e-7, n_iter_max=100, reg_W=1, verbose=0)
# Fit the estimator to the data
estimator.fit(X, y)
ax = fig.add_subplot(n_rows, n_columns, i*n_columns + j + 2)
ax.imshow(tl.to_numpy(estimator.weight_tensor_), cmap=plt.cm.OrRd, interpolation='nearest')
ax.set_axis_off()
if i == 0:
ax.set_title('Learned\nrank = {}'.format(rank))
plt.suptitle("Kruskal tensor regression")
plt.show()
def to_image(tensor):
"""A convenience function to convert from a float dtype back to uint8"""
im = tl.to_numpy(tensor).squeeze()
im -= im.min()
im /= im.max()
im *= 255
return im.astype(np.uint8)
# Subtract the projection from A_new: b <- b - a * projection
A_new = A_new - A_new * tl.reshape(projection, (tl.shape(A_new)[0], 1))
# Delete the selected row
mask.pop(max_row_idx)
A_new = A_new[mask,:]
# update the row_idx and rest_of_rows
row_idx[i] = rest_of_rows[max_row_idx]
rest_of_rows = rest_of_rows[mask]
i = i + 1
row_idx = tl.tensor(row_idx, dtype=tl.int64)
inverse = tl.solve(A[row_idx,:], tl.eye(tl.shape(A[row_idx,:])[0]))
row_idx = tl.to_numpy(row_idx)
return row_idx, inverse