Post

Replies

Boosts

Views

Activity

Reply to Tensor Flow Metal 1.2.0 on M2 Fails to converge on common toy models
@txoof import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Input, LSTM, Dense, Dropout import tensorflow as tf from tensorflow.keras.callbacks import EarlyStopping import logging # Suppress warnings logging.getLogger("tensorflow").setLevel(logging.ERROR) logging.getLogger("urllib3").setLevel(logging.ERROR) # Verify GPU availability physical_devices = tf.config.list_physical_devices('GPU') print(f"GPUs Available: {bool(physical_devices)}") # Load data with flexible features def load_data(filename, features=['Close']): df = pd.read_csv(filename, parse_dates=['DateTime']) df['DateTime'] = pd.to_datetime(df['DateTime'], format='%Y.%m.%d %H:%M:%S') required_columns = ['DateTime'] + features df_cleaned = df[required_columns].dropna().sort_values('DateTime') if df_cleaned.empty: raise ValueError("Dataset is empty after cleaning!") return df_cleaned, features # Example using multiple features selected_features = ['Open', 'High', 'Low', 'Close'] df, features = load_data('hourdata.csv', features=selected_features) print("Using features:", features) # Normalization scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(df[features]) # Sequence creation with dynamic close index def create_sequences(data, time_steps): X, y = [], [] close_idx = features.index('Close') for i in range(time_steps, len(data)): X.append(data[i-time_steps:i]) y.append(data[i, close_idx]) # Predict Close price return np.array(X), np.array(y) # Train/test split def split_data(data, time_steps, split_ratio=0.8): split_idx = int(len(data) * split_ratio) train = data[:split_idx] test = data[split_idx - time_steps:] return train, test # Enhanced prediction function def predict_price(model, scaler, df, features, time_steps): close_idx = features.index('Close') last_seq = df[features].iloc[-time_steps:] scaled_seq = scaler.transform(last_seq) X = scaled_seq.reshape(1, time_steps, len(features)) pred_scaled = model.predict(X, verbose=0) # Create dummy array for inverse transform dummy_row = np.zeros((1, len(features))) dummy_row[0, close_idx] = pred_scaled[0][0] return scaler.inverse_transform(dummy_row)[0, close_idx] # Time steps to evaluate time_steps_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 50, 100] log_df = pd.DataFrame(columns=['time_steps', 'test_loss', 'test_mae', 'current_price', 'predicted_price']) for time_steps in time_steps_list: print(f"\nProcessing time_steps={time_steps}") # Data preparation train_data, test_data = split_data(scaled_data, time_steps) X_train, y_train = create_sequences(train_data, time_steps) X_test, y_test = create_sequences(test_data, time_steps) # Reshape data X_train = X_train.reshape(-1, time_steps, len(features)) X_test = X_test.reshape(-1, time_steps, len(features)) # Model architecture model = Sequential([ Input(shape=(time_steps, len(features))), LSTM(100, return_sequences=True), Dropout(0.3), LSTM(100, return_sequences=True), Dropout(0.3), LSTM(50, return_sequences=True), Dropout(0.2), LSTM(25), Dropout(0.2), Dense(25, activation='relu'), Dense(1) ]) model.compile(optimizer='adam', loss='mse', metrics=['mae']) # Training early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True) history = model.fit( X_train, y_train, validation_data=(X_test, y_test), epochs=100, batch_size=256, verbose=0, callbacks=[early_stop] ) # Evaluation test_loss, test_mae = model.evaluate(X_test, y_test, verbose=0) current_price = df['Close'].iloc[-1] predicted_price = predict_price(model, scaler, df, features, time_steps) # Logging new_row = pd.DataFrame([{ 'time_steps': time_steps, 'test_loss': test_loss, 'test_mae': test_mae, 'current_price': current_price, 'predicted_price': predicted_price }]) log_df = pd.concat([log_df, new_row], ignore_index=True) # Save and display results log_df.to_csv('multi_feature_analysis.csv', index=False) print("\nFinal Results:") print(log_df) # Plot results with actual dates split_index = int(len(df) * 0.8) plt.figure(figsize=(14, 7)) plt.plot(df['DateTime'].iloc[split_index:], df['Close'].iloc[split_index:], label='Actual Prices') plt.title('Actual vs Model Predictions') plt.xlabel('DateTime') plt.ylabel('Price') plt.legend() plt.xticks(rotation=45) plt.tight_layout() plt.show I had code like this for LSTM , I have many versions and 2.16 just performed best but unfortunately I can’t recall as it was some time ago , and difference was in pip seem to be different for Mac OS and later versions .
Topic: Machine Learning & AI SubTopic: Core ML Tags:
Mar ’25
Reply to Using the Apple Neural Engine for MLTensor operations
I have tried the code - it throw a few warnings "Result of call to 'withMLTensorComputePolicy' is unused" "Result of call to 'shapedArray(of:)' is unused" "Initialization of immutable value 'm6' was never used; consider replacing with assignment to '' or removing it purpose of code is simply put ANE into use example "Instance Method matmul(:)" import Foundation import CoreML print("Starting ANE demonstration...") let semaphore = DispatchSemaphore(value: 0) Task { await withMLTensorComputePolicy(.init(MLComputeUnits.all)) { // Vector-vector multiplication (dot product) let v1 = MLTensor([1.0, 2.0, 3.0, 4.0]) let v2 = MLTensor([5.0, 6.0, 7.0, 8.0]) let v3 = v1.matmul(v2) let dotProduct = await v3.shapedArray(of: Float.self) print("Dot product result: \(dotProduct)") // 70.0 // Matrix-matrix multiplication let m1 = MLTensor(shape: [2, 3], scalars: [ 1, 2, 3, 4, 5, 6 ], scalarType: Float.self) let m2 = MLTensor(shape: [3, 2], scalars: [ 7, 8, 9, 10, 11, 12 ], scalarType: Float.self) let m3 = m1.matmul(m2) let matrixResult = await m3.shapedArray(of: Float.self) print("Matrix multiplication result: \(matrixResult)") // Result [[58, 64], [139, 154]] let m4 = MLTensor(randomNormal: [3, 1, 1, 4], scalarType: Float.self) let m5 = MLTensor(randomNormal: [4, 2], scalarType: Float.self) _ = m4.matmul(m5) // ignorance print("ANE operations completed successfully") } semaphore.signal() } semaphore.wait() Instrument shown same as yours power metrics as well sudo powermetrics --samplers ane_power
Topic: Machine Learning & AI SubTopic: Core ML Tags:
Mar ’25
Reply to Creating .mlmodel with Create ML Components
import CoreImage import CreateMLComponents import Foundation struct ImageRegressor { static let trainingDataURL = URL(fileURLWithPath: ("~/Desktop/bananas" as NSString).expandingTildeInPath) static let parametersURL = URL(fileURLWithPath: ("~/Desktop/parameters" as NSString).expandingTildeInPath) static let modelURL = URL(fileURLWithPath: ("~/Desktop/model.mlmodel" as NSString).expandingTildeInPath) static func train() async throws -> some Transformer<CIImage, Float> { // 1. Verify paths print("🛠️ Training data path:", trainingDataURL.path) print("🛠️ Parameters path:", parametersURL.path) print("🛠️ Model path:", modelURL.path) // 2. Create directories try FileManager.default.createDirectory(at: parametersURL, withIntermediateDirectories: true) try FileManager.default.createDirectory(at: modelURL.deletingLastPathComponent(), withIntermediateDirectories: true) // 3. Training setup print("🔍 Loading training data...") let data = try AnnotatedFiles(labeledByNamesAt: trainingDataURL, separator: "-", index: 1, type: .image) .mapFeatures(ImageReader.read) .mapAnnotations { Float($0)! } print("📊 Dataset size:", data.count) guard data.count > 0 else { throw NSError(domain: "No training data", code: -1) } let (training, validation) = data.randomSplit(by: 0.8) print("✂️ Split: \(training.count) training, \(validation.count) validation") // 4. Model training print("🏋️ Training model...") let estimator = ImageFeaturePrint().appending(LinearRegressor()) let transformer = try await estimator.fitted(to: training, validateOn: validation) // 5. Save outputs print("💾 Saving parameters...") try estimator.write(transformer, to: parametersURL.appendingPathComponent("params.bin")) print("💾 Saving model...") try transformer.export(to: modelURL) print("✅ Done! Model saved to:", modelURL.path) return transformer } } // 6. Execution wrapper func main() { let semaphore = DispatchSemaphore(value: 0) Task { do { _ = try await ImageRegressor.train() } catch { print("❌ Error:", error) } semaphore.signal() } semaphore.wait() } // 7. Start execution main() Updated to full code , thank you . Some recommendations for evolution , do I need to do separate folder like in Create ML to evaluate or increase number of iterations? Debug Console 🛠️ Training data path: /Users/xcode/Desktop/bananas 🛠️ Parameters path: /Users/xcode/Desktop/parameters 🛠️ Model path: /Users/xcode/Desktop/model.mlmodel 🔍 Loading training data... 📊 Dataset size: 273 ✂️ Split: 217 training, 56 validation 🏋️ Training model... The optimizer failed to converge after 21 iterations. 💾 Saving parameters... 💾 Saving model... ✅ Done! Model saved to: /Users/xcode/Desktop/model.mlmodel Program ended with exit code: 0
Mar ’25