Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_util.py 3.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  1. import random
  2. import numpy
  3. import tensorflow
  4. import torch
  5. import pandas
  6. import pytest
  7. from . import utils
  8. from wandb.util import json_friendly
  9. def pt_variable(nested_list, requires_grad=True):
  10. v = torch.autograd.Variable(utils.pytorch_tensor(nested_list))
  11. v.requires_grad = requires_grad
  12. return v
  13. def r():
  14. return random.random()
  15. def l(*shape):
  16. """Makes a nested list of lists with a "shape" argument like numpy,
  17. TensorFlow, etc.
  18. """
  19. if not shape:
  20. # reduce precision so we can use == for comparison regardless
  21. # of conversions between other libraries
  22. return float(numpy.float16(random.random()))
  23. else:
  24. return [l(*shape[1:]) for _ in range(shape[0])]
  25. def json_friendly_test(orig_data, obj):
  26. data, converted = json_friendly(obj)
  27. utils.assert_deep_lists_equal(orig_data, data)
  28. assert converted
  29. def tensorflow_json_friendly_test(orig_data):
  30. with tensorflow.Session().as_default() as s:
  31. json_friendly_test(orig_data, tensorflow.convert_to_tensor(orig_data))
  32. v = tensorflow.Variable(tensorflow.convert_to_tensor(orig_data))
  33. s.run(tensorflow.global_variables_initializer())
  34. json_friendly_test(orig_data, v)
  35. @pytest.mark.skipif(utils.OLD_PYTORCH, reason='0d tensors not supported until 0.4')
  36. def test_pytorch_json_0d():
  37. a = l()
  38. json_friendly_test(a, utils.pytorch_tensor(a))
  39. json_friendly_test(a, pt_variable(a))
  40. def test_pytorch_json_1d_1x1():
  41. a = l(1)
  42. json_friendly_test(a, utils.pytorch_tensor(a))
  43. json_friendly_test(a, pt_variable(a))
  44. def test_pytorch_json_1d():
  45. a = l(3)
  46. json_friendly_test(a, utils.pytorch_tensor(a))
  47. json_friendly_test(a, pt_variable(a))
  48. def test_pytorch_json_1d_large():
  49. a = l(300)
  50. json_friendly_test(a, utils.pytorch_tensor(a))
  51. json_friendly_test(a, pt_variable(a))
  52. def test_pytorch_json_2d():
  53. a = l(3, 3)
  54. json_friendly_test(a, utils.pytorch_tensor(a))
  55. json_friendly_test(a, pt_variable(a))
  56. def test_pytorch_json_2d_large():
  57. a = l(300, 300)
  58. json_friendly_test(a, utils.pytorch_tensor(a))
  59. json_friendly_test(a, pt_variable(a))
  60. def test_pytorch_json_3d():
  61. a = l(3, 3, 3)
  62. json_friendly_test(a, utils.pytorch_tensor(a))
  63. json_friendly_test(a, pt_variable(a))
  64. def test_pytorch_json_4d():
  65. a = l(3, 3, 3, 3)
  66. json_friendly_test(a, utils.pytorch_tensor(a))
  67. json_friendly_test(a, pt_variable(a))
  68. def test_pytorch_json_nd():
  69. a = l(1, 1, 1, 1, 1, 1, 1, 1)
  70. json_friendly_test(a, utils.pytorch_tensor(a))
  71. json_friendly_test(a, pt_variable(a))
  72. def test_pytorch_json_nd_large():
  73. a = l(3, 3, 3, 3, 3, 3, 3, 3)
  74. json_friendly_test(a, utils.pytorch_tensor(a))
  75. json_friendly_test(a, pt_variable(a))
  76. def test_tensorflow_json_0d():
  77. tensorflow_json_friendly_test(l())
  78. def test_tensorflow_json_1d_1x1():
  79. tensorflow_json_friendly_test(l(1))
  80. def test_tensorflow_json_1d():
  81. tensorflow_json_friendly_test(l(3))
  82. def test_tensorflow_json_1d_large():
  83. tensorflow_json_friendly_test(l(300))
  84. def test_tensorflow_json_2d():
  85. tensorflow_json_friendly_test(l(3, 3))
  86. def test_tensorflow_json_2d_large():
  87. tensorflow_json_friendly_test(l(300, 300))
  88. def test_tensorflow_json_nd():
  89. tensorflow_json_friendly_test(l(1, 1, 1, 1, 1, 1, 1, 1))
  90. def test_tensorflow_json_nd_large():
  91. tensorflow_json_friendly_test(l(3, 3, 3, 3, 3, 3, 3, 3))
  92. def test_pandas_json_2d():
  93. a = l(3, 3)
  94. json_friendly_test(a, pandas.DataFrame(a))
  95. def test_pandas_json_2d_large():
  96. a = l(300, 300)
  97. json_friendly_test(a, pandas.DataFrame(a))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...