TensorTonic
Study Plans
Study Plan
Sinusoidal Timestep Embedding
Diffusion ModelsEmbeddingsNeural Networks
Easy

Map each timestep in a batch to a fixed sinusoidal vector of length dim.

Let half be dim // 2 (integer division). For every frequency index iii from 0 to half minus 1, and every sample bbb with timestep tbt_btb​, compute

fi=exp⁡(−ln⁡(max_period)⋅ihalf),ab,i=tb fi.f_i = \exp\left(-\ln(\texttt{max\_period}) \cdot \frac{i}{\texttt{half}}\right), \qquad a_{b,i} = t_b \, f_i .fi​=exp(−ln(max_period)⋅halfi​),ab,i​=tb​fi​.

Here timesteps holds the values tbt_btb​, dim is the requested embedding length and max_period sets the slowest frequency. Row bbb of the result is the half cosine values cos⁡(ab,0),…,cos⁡(ab,half−1)\cos(a_{b,0}), \dots, \cos(a_{b,\texttt{half}-1})cos(ab,0​),…,cos(ab,half−1​) followed by the half sine values sin⁡(ab,0),…,sin⁡(ab,half−1)\sin(a_{b,0}), \dots, \sin(a_{b,\texttt{half}-1})sin(ab,0​),…,sin(ab,half−1​). When dim is odd, append one extra column of zeros at the end so every row has exactly dim entries.

Return a float32 PyTorch tensor of shape (B, dim), where B is the length of timesteps.

Examples

Input: timesteps = [0,1,10], dim = 4, max_period = 10000

Output: [[1.0,1.0,0.0,0.0],[0.540302,0.99995,0.841471,0.01],[-0.839072,0.995004,-0.544021,0.099833]]

Explanation: With dim = 4 there are two frequencies, 1 and 10000 to the power -1/2 = 0.01. Timestep 0 gives cosines of 1 and sines of 0, and timestep 10 gives cos(10), cos(0.1), sin(10) and sin(0.1).

Input: timesteps = [3,50], dim = 5, max_period = 100

Output: [[-0.989992,0.955337,0.14112,0.29552,0.0],[0.964966,0.283662,-0.262375,-0.958924,0.0]]

Input: timesteps = [999,250], dim = 6, max_period = 10

Output: [[0.99965,0.304876,-0.028925,-0.026461,-0.952392,0.999582],[0.240988,-0.980224,-0.898791,-0.970528,0.197892,-0.438377]]

Hint 1

torch.arange followed by torch.exp builds the whole geometric ladder of frequencies in one line.

Hint 2

timesteps[:, None].float() * freqs[None, :] gives a (B, half) grid of angles.

Hint 3

torch.cat along dim=1 joins column blocks, which also covers the odd-dim zero column.

Requirements

  • Use PyTorch tensor operations.
  • Return a float32 tensor whatever the dtype of timesteps.
  • Leave the timesteps tensor unchanged.

Constraints

  • timesteps is a 1-D tensor of shape (B,) with B between 1 and 4.
  • timesteps is int64 with values between 0 and 999, or float32 with non-negative finite values.
  • dim is an integer between 2 and 16.
  • max_period is a number greater than 1, given as an int or a float.
Try Similar Problems
Time-Conditioned Residual BlockMediumSpatial Self-Attention BlockMediumTiny U-Net Forward PassHardReparameterized Gaussian SampleEasyDiagonal Gaussian KL DivergenceEasyLinear Beta ScheduleEasyForward Diffusion SampleEasyNoise Prediction LossEasyActivation FunctionsEasyDouble DQN TargetEasyPerceptronEasyBatch NormalizationEasy

Accepts: array

Accepts: integer

Accepts: number

You must run your code first.

Sinusoidal Timestep Embedding

Diffusion ModelsEmbeddingsNeural Networks
Easy

Map each timestep in a batch to a fixed sinusoidal vector of length dim.

Let half be dim // 2 (integer division). For every frequency index iii from 0 to half minus 1, and every sample bbb with timestep tbt_btb​, compute

fi=exp⁡(−ln⁡(max_period)⋅ihalf),ab,i=tb fi.f_i = \exp\left(-\ln(\texttt{max\_period}) \cdot \frac{i}{\texttt{half}}\right), \qquad a_{b,i} = t_b \, f_i .fi​=exp(−ln(max_period)⋅halfi​),ab,i​=tb​fi​.

Here timesteps holds the values tbt_btb​, dim is the requested embedding length and max_period sets the slowest frequency. Row bbb of the result is the half cosine values cos⁡(ab,0),…,cos⁡(ab,half−1)\cos(a_{b,0}), \dots, \cos(a_{b,\texttt{half}-1})cos(ab,0​),…,cos(ab,half−1​) followed by the half sine values sin⁡(ab,0),…,sin⁡(ab,half−1)\sin(a_{b,0}), \dots, \sin(a_{b,\texttt{half}-1})sin(ab,0​),…,sin(ab,half−1​). When dim is odd, append one extra column of zeros at the end so every row has exactly dim entries.

Return a float32 PyTorch tensor of shape (B, dim), where B is the length of timesteps.

Examples

Input: timesteps = [0,1,10], dim = 4, max_period = 10000

Output: [[1.0,1.0,0.0,0.0],[0.540302,0.99995,0.841471,0.01],[-0.839072,0.995004,-0.544021,0.099833]]

Explanation: With dim = 4 there are two frequencies, 1 and 10000 to the power -1/2 = 0.01. Timestep 0 gives cosines of 1 and sines of 0, and timestep 10 gives cos(10), cos(0.1), sin(10) and sin(0.1).

Input: timesteps = [3,50], dim = 5, max_period = 100

Output: [[-0.989992,0.955337,0.14112,0.29552,0.0],[0.964966,0.283662,-0.262375,-0.958924,0.0]]

Input: timesteps = [999,250], dim = 6, max_period = 10

Output: [[0.99965,0.304876,-0.028925,-0.026461,-0.952392,0.999582],[0.240988,-0.980224,-0.898791,-0.970528,0.197892,-0.438377]]

Hint 1

torch.arange followed by torch.exp builds the whole geometric ladder of frequencies in one line.

Hint 2

timesteps[:, None].float() * freqs[None, :] gives a (B, half) grid of angles.

Hint 3

torch.cat along dim=1 joins column blocks, which also covers the odd-dim zero column.

Requirements

  • Use PyTorch tensor operations.
  • Return a float32 tensor whatever the dtype of timesteps.
  • Leave the timesteps tensor unchanged.

Constraints

  • timesteps is a 1-D tensor of shape (B,) with B between 1 and 4.
  • timesteps is int64 with values between 0 and 999, or float32 with non-negative finite values.
  • dim is an integer between 2 and 16.
  • max_period is a number greater than 1, given as an int or a float.
Try Similar Problems
Time-Conditioned Residual BlockMediumSpatial Self-Attention BlockMediumTiny U-Net Forward PassHardReparameterized Gaussian SampleEasyDiagonal Gaussian KL DivergenceEasyLinear Beta ScheduleEasyForward Diffusion SampleEasyNoise Prediction LossEasyActivation FunctionsEasyDouble DQN TargetEasyPerceptronEasyBatch NormalizationEasy

Accepts: array

Accepts: integer

Accepts: number

You must run your code first.