@@ -60,7 +60,7 @@ def forward(self, x):
60
60
hidden_dim = 128
61
61
num_layers = 1
62
62
num_heads = 4
63
- num_bins = 512
63
+ num_bins = 64
64
64
sequence_length = 100
65
65
66
66
# Read the robot data from the CSV file
@@ -84,6 +84,9 @@ def forward(self, x):
84
84
data = data [joints ]
85
85
trajectory_dim = len (joints )
86
86
87
+ # Normalize the LKnee joint data (per joint) / center and scale
88
+ data = (data - data .mean ()) / data .std ()
89
+
87
90
# Plot the LKnee joint data
88
91
plt .figure (figsize = (12 , 6 ))
89
92
plt .plot (data )
@@ -105,6 +108,10 @@ def forward(self, x):
105
108
num_samples = real_trajectories .size (0 )
106
109
real_trajectories = real_trajectories [torch .randperm (real_trajectories .size (0 ))]
107
110
111
+
112
+ # Limit to -1 to 1
113
+ real_trajectories = torch .tanh (real_trajectories )
114
+
108
115
# Subplot each joint, showing the first n batches
109
116
n = 1
110
117
plt .figure (figsize = (12 , 6 ))
@@ -122,20 +129,20 @@ def forward(self, x):
122
129
optimizer = torch .optim .AdamW (model .parameters (), lr = 1e-4 )
123
130
124
131
# Create batches of data
125
- batch_size = 32
132
+ batch_size = 16
126
133
127
134
# Training loop
128
- for epoch in tqdm (range (20 )): # Number of training epochs
135
+ for epoch in tqdm (range (5 )): # Number of training epochs
129
136
for batch in range (num_samples // batch_size ):
130
137
targets = real_trajectories [batch * batch_size : (batch + 1 ) * batch_size ].to (device )
131
138
132
139
optimizer .zero_grad ()
133
140
134
- # Map -1 pi - pi to 0 - 1
135
- targets_scaled = (targets + np . pi ) / ( 2 * np . pi )
141
+ # Map the data to the range 0 to 1
142
+ targets_scaled = (targets + 1 ) / 2
136
143
137
144
# Discretize into num_bins
138
- targets_binned = (targets_scaled * num_bins ).long ()
145
+ targets_binned = (targets_scaled * ( num_bins - 1 ) ).long ()
139
146
140
147
# Make floating point tensors
141
148
targets_binned = targets_binned .unsqueeze (- 1 ).to (device )
@@ -175,7 +182,8 @@ def sample_trajectory(steps=20):
175
182
probabilities .append (predicted_bin [:, - 1 , 0 ].squeeze (0 ).softmax (- 1 ).cpu ().detach ().numpy ())
176
183
177
184
# Sample top bin as the next velocity
178
- sampled_bin = torch .multinomial (predicted_bin [:, - 1 ].softmax (- 1 ).squeeze (), 1 , replacement = True )
185
+ #sampled_bin = torch.multinomial(predicted_bin[:, -1].softmax(-1).squeeze(), 1, replacement=True)
186
+ _ , sampled_bin = torch .topk (predicted_bin [:, - 1 ].softmax (- 1 ).squeeze (), 1 )
179
187
180
188
# Only keep the last predicted bin
181
189
sampled_bin = sampled_bin [:, - 1 ]
@@ -204,15 +212,11 @@ def sample_trajectory(steps=20):
204
212
sampled_trajectory = sample_trajectory (steps = 99 )
205
213
# Coverting the sampled trajectory to a numpy array
206
214
sampled_trajectory = np .array (sampled_trajectory )
207
- # Convert back to radians
208
- sampled_trajectory = (sampled_trajectory / num_bins ) * (2 * np .pi ) - np .pi
209
215
plt .figure (figsize = (12 , 6 ))
210
216
# plot the sampled trajectory for each joint in a subplot
211
217
for j in range (trajectory_dim ):
212
218
plt .subplot (3 , 4 , j + 1 )
213
219
plt .plot (sampled_trajectory [:, j ], label = "Sampled Trajectory" )
214
- # Fix limits to -pi to pi
215
- plt .ylim (- np .pi , np .pi )
216
220
plt .title (f"Joint { joints [j ]} " )
217
221
plt .legend ()
218
222
plt .show ()
0 commit comments