pub struct NNOptimizerStochasticGradientDescent { /* private fields */ }Expand description
Wraps MPSNNOptimizerStochasticGradientDescent.
Implementations§
Source§impl NNOptimizerStochasticGradientDescent
impl NNOptimizerStochasticGradientDescent
Sourcepub fn learning_rate(&self) -> f32
pub fn learning_rate(&self) -> f32
Wraps the corresponding Metal Performance Shaders method.
Sourcepub fn set_learning_rate(&self, value: f32)
pub fn set_learning_rate(&self, value: f32)
Wraps the corresponding Metal Performance Shaders method.
Sourcepub fn gradient_rescale(&self) -> f32
pub fn gradient_rescale(&self) -> f32
Wraps the corresponding Metal Performance Shaders method.
Sourcepub fn apply_gradient_clipping(&self) -> bool
pub fn apply_gradient_clipping(&self) -> bool
Wraps the corresponding Metal Performance Shaders method.
Sourcepub fn set_apply_gradient_clipping(&self, value: bool)
pub fn set_apply_gradient_clipping(&self, value: bool)
Wraps the corresponding Metal Performance Shaders method.
Sourcepub fn gradient_clip_max(&self) -> f32
pub fn gradient_clip_max(&self) -> f32
Wraps the corresponding Metal Performance Shaders method.
Sourcepub fn gradient_clip_min(&self) -> f32
pub fn gradient_clip_min(&self) -> f32
Wraps the corresponding Metal Performance Shaders method.
Sourcepub fn regularization_scale(&self) -> f32
pub fn regularization_scale(&self) -> f32
Wraps the corresponding Metal Performance Shaders method.
Sourcepub fn regularization_type(&self) -> usize
pub fn regularization_type(&self) -> usize
Wraps the corresponding Metal Performance Shaders method.
Source§impl NNOptimizerStochasticGradientDescent
impl NNOptimizerStochasticGradientDescent
Sourcepub fn new(device: &MetalDevice, learning_rate: f32) -> Option<Self>
pub fn new(device: &MetalDevice, learning_rate: f32) -> Option<Self>
Wraps a constructor on MPSNNOptimizerStochasticGradientDescent.
Examples found in repository?
37fn main() {
38 let device = MetalDevice::system_default().expect("no Metal device available");
39 let queue = device.new_command_queue().expect("command queue");
40
41 let command_buffer = queue.new_command_buffer().expect("command buffer");
42 let temporary_a =
43 State::temporary_with_buffer_size(&command_buffer, 32).expect("temporary state a");
44 let temporary_b =
45 State::temporary_with_buffer_size(&command_buffer, 64).expect("temporary state b");
46 let unique_count =
47 state_batch_increment_read_count(&[&temporary_a, &temporary_a, &temporary_b], 1);
48 assert_eq!(unique_count, 2);
49 assert_eq!(
50 temporary_a.resource_type_at_index(0),
51 state_resource_type::BUFFER
52 );
53 command_buffer.commit();
54 command_buffer.wait_until_completed();
55
56 let resource_list = StateResourceList::new().expect("resource list");
57 resource_list.append_buffer(16);
58 let persistent_state =
59 State::new_with_resource_list(&device, &resource_list).expect("persistent state");
60 assert_eq!(persistent_state.resource_count(), 1);
61 assert_eq!(persistent_state.buffer_size_at_index(0), 16);
62
63 let (gradient_buffer, gradient_vector) = vector_with_values(&device, &[0.1, -0.2]);
64 let (values_buffer, values_vector) = vector_with_values(&device, &[1.0, -1.0]);
65 let (result_buffer, result_vector) = vector_with_values(&device, &[0.0, 0.0]);
66 let optimizer = NNOptimizerStochasticGradientDescent::new(&device, 0.5).expect("sgd");
67 let base = optimizer.as_optimizer().expect("optimizer base");
68 assert_eq!(base.regularization_type(), nn_regularization_type::NONE);
69
70 let command_buffer = queue.new_command_buffer().expect("command buffer");
71 optimizer.encode_vector(
72 &command_buffer,
73 &gradient_vector,
74 &values_vector,
75 None,
76 &result_vector,
77 );
78 command_buffer.commit();
79 command_buffer.wait_until_completed();
80
81 let _ = gradient_buffer;
82 let _ = values_buffer;
83 let output = read_f32_values(&result_buffer, 2);
84 println!("{output:?}");
85}Sourcepub fn new_with_options(
device: &MetalDevice,
momentum_scale: f32,
use_nesterov_momentum: bool,
optimizer_descriptor: &NNOptimizerDescriptor,
) -> Option<Self>
pub fn new_with_options( device: &MetalDevice, momentum_scale: f32, use_nesterov_momentum: bool, optimizer_descriptor: &NNOptimizerDescriptor, ) -> Option<Self>
Wraps a constructor on MPSNNOptimizerStochasticGradientDescent.
Sourcepub fn as_optimizer(&self) -> Option<NNOptimizer>
pub fn as_optimizer(&self) -> Option<NNOptimizer>
Wraps the corresponding MPSNNOptimizerStochasticGradientDescent conversion helper.
Examples found in repository?
37fn main() {
38 let device = MetalDevice::system_default().expect("no Metal device available");
39 let queue = device.new_command_queue().expect("command queue");
40
41 let command_buffer = queue.new_command_buffer().expect("command buffer");
42 let temporary_a =
43 State::temporary_with_buffer_size(&command_buffer, 32).expect("temporary state a");
44 let temporary_b =
45 State::temporary_with_buffer_size(&command_buffer, 64).expect("temporary state b");
46 let unique_count =
47 state_batch_increment_read_count(&[&temporary_a, &temporary_a, &temporary_b], 1);
48 assert_eq!(unique_count, 2);
49 assert_eq!(
50 temporary_a.resource_type_at_index(0),
51 state_resource_type::BUFFER
52 );
53 command_buffer.commit();
54 command_buffer.wait_until_completed();
55
56 let resource_list = StateResourceList::new().expect("resource list");
57 resource_list.append_buffer(16);
58 let persistent_state =
59 State::new_with_resource_list(&device, &resource_list).expect("persistent state");
60 assert_eq!(persistent_state.resource_count(), 1);
61 assert_eq!(persistent_state.buffer_size_at_index(0), 16);
62
63 let (gradient_buffer, gradient_vector) = vector_with_values(&device, &[0.1, -0.2]);
64 let (values_buffer, values_vector) = vector_with_values(&device, &[1.0, -1.0]);
65 let (result_buffer, result_vector) = vector_with_values(&device, &[0.0, 0.0]);
66 let optimizer = NNOptimizerStochasticGradientDescent::new(&device, 0.5).expect("sgd");
67 let base = optimizer.as_optimizer().expect("optimizer base");
68 assert_eq!(base.regularization_type(), nn_regularization_type::NONE);
69
70 let command_buffer = queue.new_command_buffer().expect("command buffer");
71 optimizer.encode_vector(
72 &command_buffer,
73 &gradient_vector,
74 &values_vector,
75 None,
76 &result_vector,
77 );
78 command_buffer.commit();
79 command_buffer.wait_until_completed();
80
81 let _ = gradient_buffer;
82 let _ = values_buffer;
83 let output = read_f32_values(&result_buffer, 2);
84 println!("{output:?}");
85}Sourcepub fn momentum_scale(&self) -> f32
pub fn momentum_scale(&self) -> f32
Wraps the corresponding MPSNNOptimizerStochasticGradientDescent method.
Sourcepub fn use_nesterov_momentum(&self) -> bool
pub fn use_nesterov_momentum(&self) -> bool
Wraps the corresponding MPSNNOptimizerStochasticGradientDescent method.
Sourcepub fn encode_vector(
&self,
command_buffer: &CommandBuffer,
input_gradient_vector: &Vector,
input_values_vector: &Vector,
input_momentum_vector: Option<&Vector>,
result_values_vector: &Vector,
)
pub fn encode_vector( &self, command_buffer: &CommandBuffer, input_gradient_vector: &Vector, input_values_vector: &Vector, input_momentum_vector: Option<&Vector>, result_values_vector: &Vector, )
Wraps the corresponding MPSNNOptimizerStochasticGradientDescent encode entry point.
Examples found in repository?
37fn main() {
38 let device = MetalDevice::system_default().expect("no Metal device available");
39 let queue = device.new_command_queue().expect("command queue");
40
41 let command_buffer = queue.new_command_buffer().expect("command buffer");
42 let temporary_a =
43 State::temporary_with_buffer_size(&command_buffer, 32).expect("temporary state a");
44 let temporary_b =
45 State::temporary_with_buffer_size(&command_buffer, 64).expect("temporary state b");
46 let unique_count =
47 state_batch_increment_read_count(&[&temporary_a, &temporary_a, &temporary_b], 1);
48 assert_eq!(unique_count, 2);
49 assert_eq!(
50 temporary_a.resource_type_at_index(0),
51 state_resource_type::BUFFER
52 );
53 command_buffer.commit();
54 command_buffer.wait_until_completed();
55
56 let resource_list = StateResourceList::new().expect("resource list");
57 resource_list.append_buffer(16);
58 let persistent_state =
59 State::new_with_resource_list(&device, &resource_list).expect("persistent state");
60 assert_eq!(persistent_state.resource_count(), 1);
61 assert_eq!(persistent_state.buffer_size_at_index(0), 16);
62
63 let (gradient_buffer, gradient_vector) = vector_with_values(&device, &[0.1, -0.2]);
64 let (values_buffer, values_vector) = vector_with_values(&device, &[1.0, -1.0]);
65 let (result_buffer, result_vector) = vector_with_values(&device, &[0.0, 0.0]);
66 let optimizer = NNOptimizerStochasticGradientDescent::new(&device, 0.5).expect("sgd");
67 let base = optimizer.as_optimizer().expect("optimizer base");
68 assert_eq!(base.regularization_type(), nn_regularization_type::NONE);
69
70 let command_buffer = queue.new_command_buffer().expect("command buffer");
71 optimizer.encode_vector(
72 &command_buffer,
73 &gradient_vector,
74 &values_vector,
75 None,
76 &result_vector,
77 );
78 command_buffer.commit();
79 command_buffer.wait_until_completed();
80
81 let _ = gradient_buffer;
82 let _ = values_buffer;
83 let output = read_f32_values(&result_buffer, 2);
84 println!("{output:?}");
85}Sourcepub fn encode_matrix(
&self,
command_buffer: &CommandBuffer,
input_gradient_matrix: &Matrix,
input_values_matrix: &Matrix,
input_momentum_matrix: Option<&Matrix>,
result_values_matrix: &Matrix,
)
pub fn encode_matrix( &self, command_buffer: &CommandBuffer, input_gradient_matrix: &Matrix, input_values_matrix: &Matrix, input_momentum_matrix: Option<&Matrix>, result_values_matrix: &Matrix, )
Wraps the corresponding MPSNNOptimizerStochasticGradientDescent encode entry point.