The Pattern Weaver's Code

Medium
Deep Learning

Deep in the Crystal Cave, the enigmatic Pattern Weaver creates stunning sequences by uncovering the intricate relationships between crystals. Each crystal is marked by a unique numeric value, and the Weaver emphasizes that the true power of any crystal depends on how it interacts with all others. You have discovered N crystals, each with a specific value, and your task is to reveal their enhanced patterns by analyzing these relationships using self-attention. Given a sequence of crystals and their values, your task is to implement a simplified self-attention mechanism. For each crystal, calculate its relationship with every other crystal, compute the attention scores using the softmax function, and derive the final weighted pattern for each crystal. This Problem was made with the help of GroundZero AI

Examples

Example 1:
Input: number of crystals: 5 values: 4 2 7 1 9 dimension: 1
Output: [8.9993, 8.9638, 9.0, 8.7259, 9.0]
Explanation: The self-attention mechanism calculates relationships (attention scores) for each crystal using the given formula. These scores are converted to probabilities using the softmax function, and the final weighted pattern for each crystal is derived by summing the weighted values.

Starter Code

import numpy as np

def softmax(values):
	# Implement the softmax function
	pass

def pattern_weaver(n, crystal_values, dimension):
	# Your code here
	return np.round(x,3)
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews