|
53 | 53 | "np.random.seed(0)\n",
|
54 | 54 | "\n",
|
55 | 55 | "# Create a low-rank dense tensor from a Kruskal tensor\n",
|
56 |
| - "factor_matrices = [np.random.rand(s,R) for s in tensor_shape]\n", |
| 56 | + "factor_matrices = [np.random.rand(s, R) for s in tensor_shape]\n", |
57 | 57 | "M_true = ttb.ktensor(factor_matrices)\n",
|
58 | 58 | "X = M_true.to_tensor()"
|
59 | 59 | ]
|
|
103 | 103 | "outputs": [],
|
104 | 104 | "source": [
|
105 | 105 | "print(\"M1_info:\")\n",
|
106 |
| - "for k, v in M1_info.items(): print(f\"\\t{k}: {v}\")" |
| 106 | + "for k, v in M1_info.items():\n", |
| 107 | + " print(f\"\\t{k}: {v}\")" |
107 | 108 | ]
|
108 | 109 | },
|
109 | 110 | {
|
|
121 | 122 | "source": [
|
122 | 123 | "# Compute a solution with final ktensor stored in M2\n",
|
123 | 124 | "np.random.seed(2) # Set seed for reproducibility\n",
|
124 |
| - "M2, M2_init, _ = ttb.cp_als(X, R, maxiters=few_iters) # Will not use third output, so leaving unassigned" |
| 125 | + "M2, M2_init, _ = ttb.cp_als(\n", |
| 126 | + " X, R, maxiters=few_iters\n", |
| 127 | + ") # Will not use third output, so leaving unassigned" |
125 | 128 | ]
|
126 | 129 | },
|
127 | 130 | {
|
|
254 | 257 | "M3, _, _ = ttb.cp_als(X, R, maxiters=few_iters, init=\"nvecs\", printitn=0)\n",
|
255 | 258 | "score_M3 = M3.score(M_true)\n",
|
256 | 259 | "print(f\"Score of M2 and M_true: {score_M2[0]}\")\n",
|
257 |
| - "print(f\"Score of M3 and M_true: {score_M3[0]}\")\n" |
| 260 | + "print(f\"Score of M3 and M_true: {score_M3[0]}\")" |
258 | 261 | ]
|
259 | 262 | },
|
260 | 263 | {
|
|
271 | 274 | "metadata": {},
|
272 | 275 | "outputs": [],
|
273 | 276 | "source": [
|
274 |
| - "M4, _, M4_info = ttb.cp_als(X, R, maxiters=few_iters, init=\"nvecs\", printitn=0, dimorder=[2, 1, 0])\n", |
| 277 | + "M4, _, M4_info = ttb.cp_als(\n", |
| 278 | + " X, R, maxiters=few_iters, init=\"nvecs\", printitn=0, dimorder=[2, 1, 0]\n", |
| 279 | + ")\n", |
275 | 280 | "score_M4 = M4.score(M_true)\n",
|
276 | 281 | "print(f\"Score of M3 and M_true: {score_M3[0]}\")\n",
|
277 |
| - "print(f\"Score of M4 and M_true: {score_M4[0]}\")\n" |
| 282 | + "print(f\"Score of M4 and M_true: {score_M4[0]}\")" |
278 | 283 | ]
|
279 | 284 | },
|
280 | 285 | {
|
|
295 | 300 | "print(f\"Score of M4 and M4_rerun: {score_M4_rerun[0]}\")\n",
|
296 | 301 | "\n",
|
297 | 302 | "print(\"M4_info:\")\n",
|
298 |
| - "for k, v in M4_info.items(): print(f\"\\t{k}: {v}\")\n", |
| 303 | + "for k, v in M4_info.items():\n", |
| 304 | + " print(f\"\\t{k}: {v}\")\n", |
299 | 305 | "\n",
|
300 | 306 | "print(\"M4_rerun_info:\")\n",
|
301 |
| - "for k, v in M4_rerun_info.items(): print(f\"\\t{k}: {v}\")" |
| 307 | + "for k, v in M4_rerun_info.items():\n", |
| 308 | + " print(f\"\\t{k}: {v}\")" |
302 | 309 | ]
|
303 | 310 | },
|
304 | 311 | {
|
|
332 | 339 | "metadata": {},
|
333 | 340 | "outputs": [],
|
334 | 341 | "source": [
|
335 |
| - "# Create rank-2 tensor \n", |
| 342 | + "# Create rank-2 tensor\n", |
336 | 343 | "X2 = ttb.ktensor(\n",
|
337 | 344 | " factor_matrices=[\n",
|
338 | 345 | " np.array([[1.0, 1.0], [-1.0, -10.0]]),\n",
|
339 | 346 | " np.array([[1.0, 1.0], [-2.0, -10.0]]),\n",
|
340 | 347 | " ],\n",
|
341 |
| - " weights=np.array([1.0, 1.0])\n", |
| 348 | + " weights=np.array([1.0, 1.0]),\n", |
342 | 349 | ")\n",
|
343 | 350 | "print(f\"X2=\\n{X2}\\n\")\n",
|
344 | 351 | "\n",
|
345 | 352 | "M_fixsigns, _, _ = ttb.cp_als(X2, 2, printitn=0, init=ttb.ktensor(X2.factor_matrices))\n",
|
346 | 353 | "print(f\"M_fixsigns=\\n{M_fixsigns}\\n\") # default behavior, fixsigns called\n",
|
347 | 354 | "\n",
|
348 |
| - "M_no_fixsigns, _, _ = ttb.cp_als(X2, 2, printitn=0, init=ttb.ktensor(X2.factor_matrices), fixsigns=False)\n", |
349 |
| - "print(f\"M_no_fixsigns=\\n{M_no_fixsigns}\\n\") # fixsigns not called\n" |
| 355 | + "M_no_fixsigns, _, _ = ttb.cp_als(\n", |
| 356 | + " X2, 2, printitn=0, init=ttb.ktensor(X2.factor_matrices), fixsigns=False\n", |
| 357 | + ")\n", |
| 358 | + "print(f\"M_no_fixsigns=\\n{M_no_fixsigns}\\n\") # fixsigns not called" |
350 | 359 | ]
|
351 | 360 | },
|
352 | 361 | {
|
|
359 | 368 | ]
|
360 | 369 | }
|
361 | 370 | ],
|
362 |
| - "metadata": {}, |
| 371 | + "metadata": { |
| 372 | + "language_info": { |
| 373 | + "name": "python" |
| 374 | + } |
| 375 | + }, |
363 | 376 | "nbformat": 4,
|
364 | 377 | "nbformat_minor": 2
|
365 | 378 | }
|
0 commit comments